blufish Posted November 20, 2008 Share Posted November 20, 2008 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 Link to comment https://forums.phpfreaks.com/topic/133475-solved-how-to-parse-sections/ Share on other sites More sharing options...
Garethp Posted November 20, 2008 Share Posted November 20, 2008 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 Link to comment https://forums.phpfreaks.com/topic/133475-solved-how-to-parse-sections/#findComment-694220 Share on other sites More sharing options...
blufish Posted November 21, 2008 Author Share Posted November 21, 2008 I used: $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 Link to comment https://forums.phpfreaks.com/topic/133475-solved-how-to-parse-sections/#findComment-695185 Share on other sites More sharing options...
bobbinsbro Posted November 21, 2008 Share Posted November 21, 2008 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: $stuff = preg_match('~\[1\]*\[/1\]~',$pagetext); Link to comment https://forums.phpfreaks.com/topic/133475-solved-how-to-parse-sections/#findComment-695187 Share on other sites More sharing options...
blufish Posted November 21, 2008 Author Share Posted November 21, 2008 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? Link to comment https://forums.phpfreaks.com/topic/133475-solved-how-to-parse-sections/#findComment-695191 Share on other sites More sharing options...
JasonLewis Posted November 21, 2008 Share Posted November 21, 2008 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>"; Link to comment https://forums.phpfreaks.com/topic/133475-solved-how-to-parse-sections/#findComment-695196 Share on other sites More sharing options...
blufish Posted November 21, 2008 Author Share Posted November 21, 2008 That looks good, but what variable is the titles stored in? I only need the titles, not the content. Link to comment https://forums.phpfreaks.com/topic/133475-solved-how-to-parse-sections/#findComment-695197 Share on other sites More sharing options...
JasonLewis Posted November 21, 2008 Share Posted November 21, 2008 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>"; Link to comment https://forums.phpfreaks.com/topic/133475-solved-how-to-parse-sections/#findComment-695199 Share on other sites More sharing options...
blufish Posted November 21, 2008 Author Share Posted November 21, 2008 Thanks, I made some changes and now it works great! Link to comment https://forums.phpfreaks.com/topic/133475-solved-how-to-parse-sections/#findComment-695216 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.