note 96483 modified in function.strip-tags by danbrown

From: Date: Tue, 02 Mar 2010 15:36:20 +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-166529@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 = false) { $content = ''; 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) $content = '(.+</'.$tag.'[^>]*>|)'; $str = preg_replace('#</?'.$tag.'[^>]*>'.$content.'#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. http://php.net/manual/en/function.strip-tags.php

« previous php.notes (#166529) next »