note 96483 modified in function.strip-tags by danbrown
| From: | danbrown@php.net | Date: | Mon, 01 Mar 2010 15:57:13 +0000 |
| Subject: | note 96483 modified in function.strip-tags by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166481@lists.php.net to get a copy of this message | ||
Here is support for stripping content for the reverse strip_tags function:
<?php
function strip_only($str, $tags, $stripContent = '') {
if(!is_array($tags)) {
$tags = (strpos($str, '>') !== false ? explode('>',
str_replace('<', '', $tags)) : array($tags));
if(end($tags) == '') array_pop($tags);
}
foreach($tags as $tag) {
if ($stripContent)
$stripContent = '(.+</'.$tag.'[^>]*>|)';
$str =
preg_replace('#</?'.$tag.'[^>]*>'.$stripContent.'#is',
'', $str);
}
return $str;
}
$str = '<font color="red">red</font> text';
$tags = 'font';
$a = strip_only($str, $tags); // red text
$b = strip_only($str, $tags, true); // text
?>
Note this function always assumed no two tags start the same way (e.g. <bla> and <blas>)
and therefore censors blas along with bla.
--was--
Here is support for stripping content for the reverse strip_tags function:
<?php
function strip_only($str, $tags, $stripContent = '') {
if(!is_array($tags)) {
$tags = (strpos($str, '>') !== false ? explode('>',
str_replace('<', '', $tags)) : array($tags));
if(end($tags) == '') array_pop($tags);
}
foreach($tags as $tag) {
if ($stripContent)
$stripContent = '(.+</'.$tag.'[^>]*>|)';
$str =
preg_replace('#</?'.$tag.'[^>]*>'.$stripContent.'#is',
'', $str);
}
return $str;
}
$str = '<font color="red">red</font> text';
$tags = 'font';
$a = strip_only($str, $tags); // red text
$b = strip_only($str, $tags, true); // text
?>
Note this function always assumed no two tags start the same way (e.g. <bla> and <blas>)
and therefore censors blas along with bla. Does anyone know how to support it?
http://php.net/manual/en/function.strip-tags.php