funkyres Posted February 12, 2009 Share Posted February 12, 2009 Let's say I have the following string - <?php $string="some of our Pacific Gopher <b>Snake</b> populations receive some gene flow from the Great Basin Gopher <b>Snake</b> (Pituophis catenifer deserticola). The Great Basin Gopher <b>Snake</b> does intergrade with the Pacific Gopher <b>Snake</b> to the north of us in Siskiyou"; ?> I want to break it up into an array like this: <?php $foo[]="some of our Pacific Gopher "; $foo[]="<b>Snake</b>"; $foo[]=" populations receive some gene flow from the Great Basin Gopher "; $foo[]="<b>Snake</b>"; $foo[]=" (Pituophis catenifer deserticola). The Great Basin Gopher "; $foo[]="<b>Snake</b>"; $foo[]=" does intergrade with the Pacific Gopher "; $foo[]="<b>Snake</b>"; $foo[]=" to the north of us in Siskiyou"; ?> The text in the bold tags won't always be the same (otherwise I could probably just use explode). I do need to preserve the text within the bold tags, and the bold tags. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/144863-parsing-html/ Share on other sites More sharing options...
drisate Posted February 12, 2009 Share Posted February 12, 2009 How abbout something like this <?php $check ="<b>Snake</b>"; $string="some of our Pacific Gopher <b>Snake</b> populations receive some gene flow from the Great Basin Gopher <b>Snake</b> (Pituophis catenifer deserticola). The Great Basin Gopher <b>Snake</b> does intergrade with the Pacific Gopher <b>Snake</b> to the north of us in Siskiyou"; $str = explode ($check, $string); foreach ($str as $array){ $new[]=$array; $new[]=$check; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/144863-parsing-html/#findComment-760197 Share on other sites More sharing options...
funkyres Posted February 12, 2009 Author Share Posted February 12, 2009 The problem is I don't know ahead of time what is in the bold tags. I might try to use the xml DOM to do it. Quote Link to comment https://forums.phpfreaks.com/topic/144863-parsing-html/#findComment-760203 Share on other sites More sharing options...
sasa Posted February 12, 2009 Share Posted February 12, 2009 try <?php $check ="<b>Snake</b>"; $sr = array('<b>','</b>'); $re = array('|||<b>','</b>|||'); $string="some of our Pacific Gopher <b>Snake</b> populations receive some gene flow from the Great Basin Gopher <b>Snake</b> (Pituophis catenifer deserticola). The Great Basin Gopher <b>Snake</b> does intergrade with the Pacific Gopher <b>Snake</b> to the north of us in Siskiyou"; $str = explode ('|||', str_replace($sr, $re, $string)); print_r($str); ?> Quote Link to comment https://forums.phpfreaks.com/topic/144863-parsing-html/#findComment-760231 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.