Jump to content

Parsing html ??


funkyres

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/144863-parsing-html/
Share on other sites

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;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/144863-parsing-html/#findComment-760197
Share on other sites

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/144863-parsing-html/#findComment-760231
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.