Jump to content

parsing a string between 2 strings.


TexasMd91

Recommended Posts

I currently have a string like this.

<p><span style="font-size:0.8em;">songs played:</span> 152</p>
<p><span style="font-size:0.8em;">radio count:</span> 1</p>

 

Now, I need to parse it and find out what is between

radio count:</span>  

and

</p>

 

I thought about using the strpos to find where exactly the string is, and then using that to grab the data inbetween. However, it would find the </p> after the 152 instead of the </p> after the 1.

 

Any suggestions on how to achieve this?

Link to comment
https://forums.phpfreaks.com/topic/119846-parsing-a-string-between-2-strings/
Share on other sites

Will get you the numeric value of the radio count:

$string = '<p><span style="font-size:0.8em;">songs played:</span> 152</p><p><span style="font-size:0.8em;">radio count:</span> 1</p>';
$string = strip_tags($string);
preg_match('/radio count: ([0-9]+)/', $string, $matches);
print $matches[1];

Couldn't you get put all the information you want to access in the 'radio count:' span tag and just used the .innerHTML property to get the text found in the span tag?

The problem is, its not my HTML. I am using file_get_contents on a website to retrieve stats.

 

Thanks neil.johnson, that worked :)

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.