Jump to content

strip tags functions


ohdang888

Recommended Posts

I want to strip content of certain html, and of the allowable html, i want to search through the attributes. I found the script below on php.net

 

I have content set as this:

test<img src="http://www.google.com/logos/spring09.gif" title="cool"><a href="#" Onclick="test();">test</a>

 

and it only echos this:

test<img src="http://www.google.com/logos/spring09.gif">

 

In other words, it comes across an attribute it doesn't allow, and it stops every bit of html after that point, instead of just striping that html. i obviosuly don't like that.

 

How can i change it?

 

Thanks

 

What i am using:

<?php
echo  strip_tags_attributes($contents,'<br><b><u><span><div><table><tr><td><strong><em><a><img><form><input>','href,rel,src,width,height,color,size'); 
?>

 

The script:

<?php
function strip_tags_attributes($string,$allowtags=NULL,$allowattributes=NULL){
    if($allowattributes){
        if(!is_array($allowattributes))
            $allowattributes = explode(",",$allowattributes);
        if(is_array($allowattributes))
            $allowattributes = implode("|",$allowattributes);
        $rep = '/([^>]*) ('.$allowattributes.')(=)(\'.*\'|".*")/i';
        $string = preg_replace($rep, '$1 $2_$4', $string);
    }
    $string = preg_replace('/([^>]*) (.*)(=\'.*\'|=".*")(.*)/i', '$1$4', $string);
    $rep = '/([^>]*) ('.$allowattributes.')(_)(\'.*\'|".*")/i';
    if($allowattributes)
        $string = preg_replace($rep, '$1 $2=$4', $string);
    return strip_tags($string,$allowtags);
}
?> 

Link to comment
https://forums.phpfreaks.com/topic/150432-strip-tags-functions/
Share on other sites

  • 1 year later...

Hello,

I need a function like a strip_tags_attributes() and I`ve found this version, and...

it has a bug...

when you want accept usig <b> and you write:

echo strip_tags_attributes($q,'<b>');

user can use JavaScript in onclick etc when he doesn`t use comma

f.e.:

<b onclick=alert('w')>testik</b>

 

I know this is mistake of browser, but e.g. in my Opera 10.51 this hack is working

in Chrome 4.1 also...

I suppose that in all of browsers that will be working ;)

 

 

Archived

This topic is now archived and is closed to further replies.

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