Author Topic: [SOLVED] How to parse sections?  (Read 198 times)

0 Members and 1 Guest are viewing this topic.

Offline blufishTopic starter

  • Enthusiast
  • Gender: Male
  • Wait! you weren't supposed to see this text!
    • View Profile
    • Frozenoven
[SOLVED] How to parse sections?
« on: November 20, 2008, 05:15:03 AM »
I want to know how to parse this... I know there is a way, please help me.
Someone posts:

[1]Section 1[/1]
Blah Blah Blah
[1]Section 2[/1]
Blah Blah Blah
[1]Section 3[/1]

What I want to do is take all the text inbetween the [1]s and [/1]s and put it into an array, like:

$array1 = "Section 1";
$array2 = "Section 2";
$array3 = "Section 3";

Then I need the text to be made into something like this.

[1]Section 1=Section 1[/1]
Blah Blah Blah
[1]Section 2=Section 2[/1]
Blah Blah Blah
[1]Section 3=Section 3[/1]

How can this be done?

Thanks
Visit My Website!

Offline Garethp

  • Devotee
  • Gender: Male
  • ($2b) || ! ($2b)
    • View Profile
Re: How to parse sections?
« Reply #1 on: November 20, 2008, 05:41:22 AM »
Well, you could use preg_match to detect all the items between [1] and [/1] and you could then use Str_Replace to find all those variables and replace them. If str_replace doesn't work, try preg_replace instead. The main difference is preg_replace let's you use RegEx. This naturally slows it down so see which one suits your needs
The Three Rules of Getting Assistance
1. Google first
2. Ask second
3. Then buy me some chocolate

Offline blufishTopic starter

  • Enthusiast
  • Gender: Male
  • Wait! you weren't supposed to see this text!
    • View Profile
    • Frozenoven
Re: How to parse sections?
« Reply #2 on: November 21, 2008, 02:49:44 AM »
I used:
Code: [Select]
$stuff = preg_match("[1]*[/1]",$pagetext);
To try to put the put the stuff inbetween [1] and [/1] into $stuff as an array but it didn't do anything.  Any Ideas? Did I do it wrong?

Thanks
Visit My Website!

Offline bobbinsbro

  • Enthusiast
  • Gender: Male
    • View Profile
Re: How to parse sections?
« Reply #3 on: November 21, 2008, 03:30:13 AM »
i believe [] are control chatacters in regex, and so must be escaped. also, when using preg function i think you have to have an opening/closing character around the string. here i use ~. try this:
Code: [Select]
$stuff = preg_match('~\[1\]*\[/1\]~',$pagetext);

Offline blufishTopic starter

  • Enthusiast
  • Gender: Male
  • Wait! you weren't supposed to see this text!
    • View Profile
    • Frozenoven
Re: How to parse sections?
« Reply #4 on: November 21, 2008, 03:37:01 AM »
when I put print_r($stuff); it gave me a 0, it should have said

$_stuff[0] = Section 1
$_stuff[1] = Section 2
$_stuff[2] = Section 3

Any ideas?
Visit My Website!

Offline ProjectFear

  • Addict
  • Gender: Male
  • Have no fear of perfection, you'll never reach it.
    • View Profile
    • JCLmedia
Re: How to parse sections?
« Reply #5 on: November 21, 2008, 03:44:58 AM »
Try this out, not sure if that's what you wanted though.

$input = <<<html
[1]Section 1[/1]
Blah Blah Blah
[1]Section 2[/1]
Blah Blah Blah
[1]Section 3[/1]
html;

preg_match_all("#\[1\](.+)\[/1\](.*[^\[1\]]*)#i"$input$matches);
$sections = array();
for(
$i 0$i count($matches[1]); $i++){
	
$sections[] = array("title" => $matches[1][$i], "content" => trim($matches[2][$i]));
}

echo 
"<pre>"print_r($sections), "</pre>";
I help because I enjoy helping others. But please help me (and others) by posting understandable questions! Oh and use the CODE tags.

-------------------------------------------------

Daring ideas... They are like chessmen when moved forward. They may be beaten, but they may start a winning game...

Offline blufishTopic starter

  • Enthusiast
  • Gender: Male
  • Wait! you weren't supposed to see this text!
    • View Profile
    • Frozenoven
Re: How to parse sections?
« Reply #6 on: November 21, 2008, 03:48:14 AM »
That looks good, but what variable is the titles stored in? I only need the titles, not the content.
Visit My Website!

Offline ProjectFear

  • Addict
  • Gender: Male
  • Have no fear of perfection, you'll never reach it.
    • View Profile
    • JCLmedia
Re: How to parse sections?
« Reply #7 on: November 21, 2008, 03:51:01 AM »
Okay, in that case then, this should work:

$input = <<<html
[1]Section 1[/1]
Blah Blah Blah
[1]Section 2[/1]
Blah Blah Blah
[1]Section 3[/1]
html;

preg_match_all("#\[1\](.+)\[/1\]#i"$input$matches);
$sections = array();
for(
$i 0$i count($matches[1]); $i++){
	
$sections[] = $matches[1][$i];
}

echo 
"<pre>"print_r($sections), "</pre>";
I help because I enjoy helping others. But please help me (and others) by posting understandable questions! Oh and use the CODE tags.

-------------------------------------------------

Daring ideas... They are like chessmen when moved forward. They may be beaten, but they may start a winning game...

Offline blufishTopic starter

  • Enthusiast
  • Gender: Male
  • Wait! you weren't supposed to see this text!
    • View Profile
    • Frozenoven
Re: How to parse sections?
« Reply #8 on: November 21, 2008, 04:22:44 AM »
Thanks, I made some changes and now it works great!
Visit My Website!

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.