Jump to content

Extracting only text within a certain tag


denhamd2

Recommended Posts

I have a problem... I have text in a div class "schedulediv" which contains content which is dragged in dynamically and echo'ed as the variable $schedule. Basically all the headers are in <span> tags with the style "font-weight:bold" on them. I want to hide all the non-bold text, ie all the text in the div class .schedule that doesn't have a <span> tag around it. If possible I would like to to this without resorting to CSS... is it possible to extract all the <span> classes in the string $schedule and echo them with a <br> tag after each?

How's about:

 

<?php
$schedule = 'Test<span>Some Text</span> Text not in a span tag <span> Bit more text here inside a span</span> Blah Blah Blah';
$schedule = preg_replace('|</span>(.*?)<span>|','</span><span>',$schedule);//strip out anything between span tags
$schedule = strstr($schedule,'<span>');//take out anything before the first span pag
$rev = strrev($schedule);//reverse string, to find 'first' occurrance of closing span tag
$rev = strstr($rev,strrev('</span>'));//take out anything before it 
$schedule = strrev($rev);
echo $schedule
?>

This is a bit neater:

 

<?php
$schedule = 'Test<span>Some Text</span> Text not in a span tag <span> Bit more text here inside a span</span> Blah Blah Blah';
preg_match_all('|<span>(.*?)</span>|',$schedule,$matches);//put anything between span tags into an array
$schedule = implode('',$matches[0]);//join them together
echo $schedule;
?>

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.