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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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