Helmet Posted April 1, 2008 Share Posted April 1, 2008 Hey -- trying to find a function that can read in a long string like "Hello this is <span class="someclass">my</span> string" and have it return "Hello this is string". In other words nuke all the spans of that class. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/99061-strip-all-spans-of-a-certain-class-in-content/ Share on other sites More sharing options...
soycharliente Posted April 1, 2008 Share Posted April 1, 2008 I am not an expert, but try this ... <?php $string = 'This is a <span class="foo">test</span> string.'; $content = preg_replace('/<.+?>/', '', $string); ?> Quote Link to comment https://forums.phpfreaks.com/topic/99061-strip-all-spans-of-a-certain-class-in-content/#findComment-506923 Share on other sites More sharing options...
Helmet Posted April 1, 2008 Author Share Posted April 1, 2008 Ah sorry.. the sample I posted was a sample of what I was trying to remove from a much larger string containing lots of html, so I'm just trying to remove the tags and content contained within, that have a certain class. I was just looking at another regular expressions match for square brackets, since all the spans I'm talking about are: <span class="foo">[ random content I can't predict ] </span> $content = preg_replace('/\\[[^\\]]*\\]/', '', $content); But this is interfering with the html in a part of the page that does not have square brackets. Quote Link to comment https://forums.phpfreaks.com/topic/99061-strip-all-spans-of-a-certain-class-in-content/#findComment-506930 Share on other sites More sharing options...
MadTechie Posted April 1, 2008 Share Posted April 1, 2008 use $html = strip_tags($html); Quote Link to comment https://forums.phpfreaks.com/topic/99061-strip-all-spans-of-a-certain-class-in-content/#findComment-506949 Share on other sites More sharing options...
Helmet Posted April 1, 2008 Author Share Posted April 1, 2008 I'm not trying to strip all the tags out of the string, only those that have a particular class. The string is an html page. Thanks for replying though.. Quote Link to comment https://forums.phpfreaks.com/topic/99061-strip-all-spans-of-a-certain-class-in-content/#findComment-506957 Share on other sites More sharing options...
MadTechie Posted April 1, 2008 Share Posted April 1, 2008 ahh so this then $content = preg_replace('%<span class="foo">(.*?)</span>%sm', '\1', $content); Quote Link to comment https://forums.phpfreaks.com/topic/99061-strip-all-spans-of-a-certain-class-in-content/#findComment-506960 Share on other sites More sharing options...
Helmet Posted April 2, 2008 Author Share Posted April 2, 2008 Unfortunately that doesn't seem to be working Quote Link to comment https://forums.phpfreaks.com/topic/99061-strip-all-spans-of-a-certain-class-in-content/#findComment-506982 Share on other sites More sharing options...
soycharliente Posted April 2, 2008 Share Posted April 2, 2008 To get you started thinking and to help others find better ways ... <?php function myReplace($html, $bad_class) { $pos = strpos($html, $bad_class); while ($pos !== FALSE) { // find last instance of '<' in the string up until $bad_class $start = strrpos(substr($html, 0, $pos), '<'); // find last instance of '>' in the string up until $bad_class $stop = strrpos(substr($html, 0, $pos), '>'); // replace the text from '<' to '>' with nothing $html = substr_replace($html, '', $start, $stop); // find first instance of '<' in the string after $bad_class $start = strpos(substr($html, $pos), '<'); // find first instance of '>' in the string after $bad_class $stop = strpos(substr($html, $pos), '>'); // replace the text from '<' to '>' with nothing $html = substr_replace($html, '', $start, $stop); // keep looking for more $bad_class $pos = strpos($html, $bad_class); } return $html; } $html = 'Lots of <span class="foo">data</span> with multiple <span class="bar">classes</span> in it.'; $bad_class = 'class="bar"'; $html = myReplace($html); ?> Now, this will only work if there are no '<' or '>' in the text that is between the tags you are trying to eliminate. It should remove the tag before, but not it's corresponding closing tag if there is a '<' or '>' wrapped inside the tag. You could argue that you could make it better by searching for the tag type once you found the '<' or '>' and then trying to find it's corresponding closing tag based on what tag it is. But even then, what if you were trying to write code wrapped in the tag. You could find a scenario to screw over almost anything. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/99061-strip-all-spans-of-a-certain-class-in-content/#findComment-507198 Share on other sites More sharing options...
MadTechie Posted April 2, 2008 Share Posted April 2, 2008 Unfortunately that doesn't seem to be working I jusr tested it, seams fine! Quote Link to comment https://forums.phpfreaks.com/topic/99061-strip-all-spans-of-a-certain-class-in-content/#findComment-507223 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.