Please login or register.

Login with username, password and session length
Advanced search  

News:

We are constantly trying to improve phpfreaks and these forums, so feel free to go to the PHPFreaks Comments/Suggestions board and point out anything you'd like to see different!

Maintenance Notice

PHPFreaks has successfully moved to a new Dedicated Server, hosted by Server Powered. Please help support future upgrades by Donating.

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

0 Members and 1 Guest are viewing this topic.

blufish

  • Enthusiast
  • Offline Offline
  • Gender: Male
  • Posts: 189
  • Wait! you weren't supposed to see this text!
    • View Profile
    • WWW
[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
Logged

Visit My Website!

Garethp

  • Enthusiast
  • Offline Offline
  • Gender: Male
  • Posts: 136
  • (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
Logged

The Three Rules of Getting Assistance
1. Google first
2. Ask second
3. Then buy me some chocolate

blufish

  • Enthusiast
  • Offline Offline
  • Gender: Male
  • Posts: 189
  • Wait! you weren't supposed to see this text!
    • View Profile
    • WWW
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
Logged

Visit My Website!

bobbinsbro

  • Enthusiast
  • Offline Offline
  • Gender: Male
  • Posts: 362
    • 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);
Logged

blufish

  • Enthusiast
  • Offline Offline
  • Gender: Male
  • Posts: 189
  • Wait! you weren't supposed to see this text!
    • View Profile
    • WWW
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?
Logged

Visit My Website!

ProjectFear

  • Addict
  • Offline Offline
  • Gender: Male
  • Posts: 2,633
    • View Profile
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>";
Logged

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...

blufish

  • Enthusiast
  • Offline Offline
  • Gender: Male
  • Posts: 189
  • Wait! you weren't supposed to see this text!
    • View Profile
    • WWW
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.
Logged

Visit My Website!

ProjectFear

  • Addict
  • Offline Offline
  • Gender: Male
  • Posts: 2,633
    • View Profile
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>";
Logged

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...

blufish

  • Enthusiast
  • Offline Offline
  • Gender: Male
  • Posts: 189
  • Wait! you weren't supposed to see this text!
    • View Profile
    • WWW
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!
Logged

Visit My Website!
Pages: [1]
 

Page created in 0.055 seconds with 18 queries.