note 97386 added to function.strip-tags
| From: | php at wizap dot com | Date: | Fri, 16 Apr 2010 23:58:50 +0000 |
| Subject: | note 97386 added to function.strip-tags | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168337@lists.php.net to get a copy of this message | ||
This could be overkill but this strips all HTML tags and gives you the option to preserve the ones
you define. It also takes into account tags like <script> removing all the javascript, too!
You can also strip out all the content between any tag that has an opening and closing tag, like
<table>, <object>, etc.
Have fun. Let me know what you think. http://zt.a.atr.im
<?php
function remove_HTML($s , $keep = '' , $expand =
'script|style|noframes|select|option'){
/**///prep the string
$s = ' ' . $s;
/**///initialize keep tag logic
if(strlen($keep) > 0){
$k = explode('|',$keep);
for($i=0;$i<count($k);$i++){
$s = str_replace('<' . $k[$i],'[{(' . $k[$i],$s);
$s = str_replace('</' . $k[$i],'[{(/' . $k[$i],$s);
}
}
//begin removal
/**///remove comment blocks
while(stripos($s,'<!--') > 0){
$pos[1] = stripos($s,'<!--');
$pos[2] = stripos($s,'-->', $pos[1]);
$len[1] = $pos[2] - $pos[1] + 3;
$x = substr($s,$pos[1],$len[1]);
$s = str_replace($x,'',$s);
}
/**///remove tags with content between them
if(strlen($expand) > 0){
$e = explode('|',$expand);
for($i=0;$i<count($e);$i++){
while(stripos($s,'<' . $e[$i]) > 0){
$len[1] = strlen('<' . $e[$i]);
$pos[1] = stripos($s,'<' . $e[$i]);
$pos[2] = stripos($s,$e[$i] . '>', $pos[1] + $len[1]);
$len[2] = $pos[2] - $pos[1] + $len[1];
$x = substr($s,$pos[1],$len[2]);
$s = str_replace($x,'',$s);
}
}
}
/**///remove remaining tags
while(stripos($s,'<') > 0){
$pos[1] = stripos($s,'<');
$pos[2] = stripos($s,'>', $pos[1]);
$len[1] = $pos[2] - $pos[1] + 1;
$x = substr($s,$pos[1],$len[1]);
$s = str_replace($x,'',$s);
}
/**///finalize keep tag
for($i=0;$i<count($k);$i++){
$s = str_replace('[{(' . $k[$i],'<' . $k[$i],$s);
$s = str_replace('[{(/' . $k[$i],'</' . $k[$i],$s);
}
return trim($s);
}
?>
----
Server IP: 192.41.40.227
Probable Submitter: 76.8.195.98
----
Manual Page -- http://www.php.net/manual/en/function.strip-tags.php
Edit -- https://master.php.net/note/edit/97386
Del: integrated -- https://master.php.net/note/delete/97386/integrated
Del: useless -- https://master.php.net/note/delete/97386/useless
Del: bad code -- https://master.php.net/note/delete/97386/bad+code
Del: spam -- https://master.php.net/note/delete/97386/spam
Del: non-english -- https://master.php.net/note/delete/97386/non-english
Del: in docs -- https://master.php.net/note/delete/97386/in+docs
Del: other reasons-- https://master.php.net/note/delete/97386
Reject -- https://master.php.net/note/reject/97386
Search -- https://master.php.net/manage/user-notes.php