hamza Posted March 10, 2010 Share Posted March 10, 2010 i want to strip span tag as well as anchor tag this code is in my php post array <br><br><span id="1.612802118875386"><b>Other Page: </b>Matte Card<br><b>Gramage: </b>80 gram<br>sdafsdaf<br> <a href="#" onclick="removetext(1.612802118875386)"> Remove </a> </span><br><br><span id="1.014127052347593"><b>Other Page: </b>Matte Card<br><b>Gramage: </b>100 gram<br>sdfasfsdfdsfsdafsdafas<br> <a href="#" onclick="removetext(1.014127052347593)"> Remove </a> </span> strip tag funtion is not workign for me. Link to comment https://forums.phpfreaks.com/topic/194788-strip-tags/ Share on other sites More sharing options...
jdorma0 Posted March 10, 2010 Share Posted March 10, 2010 Courtesy of PHP.net: <?php function strip_only($str, $tags) { if(!is_array($tags)) { $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); if(end($tags) == '') array_pop($tags); } foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str); return $str; } $str = '<p style="text-align:center">Paragraph</p><strong>Bold</strong><br/><span style="color:red">Red</span><h1>Header</h1>'; echo strip_only($str, array('p', 'h1')); echo strip_only($str, '<p><h1>'); ?> http://us.php.net/manual/en/function.strip-tags.php Both return: Paragraph<strong>Bold</strong><br/><span style="color:red">Red</span>Header So, in your case, call the function as: echo strip_only($str,array('a','span')); //echoing so you can see the output Hope that helps! Joey Link to comment https://forums.phpfreaks.com/topic/194788-strip-tags/#findComment-1024292 Share on other sites More sharing options...
Psycho Posted March 10, 2010 Share Posted March 10, 2010 This is a slightly improved version of that function which can strip all the tags at once without a foreach loop. I also removed the section to handle <> in the value when passed as a string. I figured if the values can't include those when passing the values as an array then the same rules should apply when passing as a string. Instead, I modified it to allow multiple values to be passed in a single comma separated string: <?php function strip_only($str, $tags) { if(!is_array($tags)) { $tags = explode(',',$tags); } $tagsPattern = implode('|', $tags); return preg_replace("#</?({$tagsPattern})[^>]*>#is", '', $str); } $str = ' <br><br><span id="1.612802118875386"><b>Other Page: </b>Matte Card<br><b>Gramage: </b>80 gram<br>sdafsdaf<br> <a href="#" onclick="removetext(1.612802118875386)"> Remove </a> </span><br><br><span id="1.014127052347593"><b>Other Page: </b>Matte Card<br><b>Gramage: </b>100 gram<br>sdfasfsdfdsfsdafsdafas<br> <a href="#" onclick="removetext(1.014127052347593)"> Remove </a> </span>'; echo strip_only($str, 'span,a'); // OR echo strip_only($str, array('span', 'a')); ?> Link to comment https://forums.phpfreaks.com/topic/194788-strip-tags/#findComment-1024302 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.