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?

Link to comment
Share on other sites

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

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