Jump to content

strip tags


hamza

Recommended Posts

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

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

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