note 97386 modified in function.strip-tags by danbrown

From: Date: Fri, 23 Sep 2011 16:00:55 +0000
Subject: note 97386 modified in function.strip-tags by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183375@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. <?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); } ?> --was-- 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); } ?> http://php.net/manual/en/function.strip-tags.php

« previous php.notes (#183375) next »