note 96483 deleted from function.strip-tags by aharvey

From: Date: Thu, 20 Sep 2012 12:12:07 +0000
Subject: note 96483 deleted from function.strip-tags by aharvey
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190921@lists.php.net to get a copy of this message
Note Submitter: LWC ---- 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.

« previous php.notes (#190921) next »