note 89654 deleted from function.strip-tags by danbrown

From: Date: Tue, 31 Mar 2009 18:05:20 +0000
Subject: note 89654 deleted from function.strip-tags by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-152658@lists.php.net to get a copy of this message
Note Submitter: nick at optixsolutions dot co dot uk ---- Function to strip tags and attributes, but with allowable attributes <?php function strip_tags_attributes($string,$allowtags=NULL,$allowattributes=NULL){ if($allowattributes){ if(!is_array($allowattributes)) $allowattributes = explode(",",$allowattributes); if(is_array($allowattributes)) $allowattributes = implode("|",$allowattributes); $rep = '/([^>]*) ('.$allowattributes.')(=)(\'.*\'|".*")/i'; $string = preg_replace($rep, '$1 $2_-_-$4', $string); } $string = preg_replace('/([^>]*) (.*)(=\'.*\'|=".*")(.*)/i', '$1$4', $string); $rep = '/([^>]*) ('.$allowattributes.')(_-_-)(\'.*\'|".*")/i'; if($allowattributes) $string = preg_replace($rep, '$1 $2=$4', $string); return strip_tags($string,$allowtags); } ?> Usage: Allowable attributes can be comma seperated or array Example: <?php strip_tags_attributes($string,'<strong><em><a>','href,rel'); ?>

« previous php.notes (#152658) next »