denhamd2 Posted January 17, 2008 Share Posted January 17, 2008 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? Link to comment https://forums.phpfreaks.com/topic/86433-extracting-only-text-within-a-certain-tag/ Share on other sites More sharing options...
GingerRobot Posted January 17, 2008 Share Posted January 17, 2008 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 ?> Link to comment https://forums.phpfreaks.com/topic/86433-extracting-only-text-within-a-certain-tag/#findComment-441707 Share on other sites More sharing options...
GingerRobot Posted January 17, 2008 Share Posted January 17, 2008 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; ?> Link to comment https://forums.phpfreaks.com/topic/86433-extracting-only-text-within-a-certain-tag/#findComment-441709 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.