note 56082 deleted from function.strip-tags by danbrown

From: Date: Sun, 17 May 2009 16:02:23 +0000
Subject: note 56082 deleted from function.strip-tags by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154744@lists.php.net to get a copy of this message
Note Submitter: daneel at neezine dot net ---- Remove attributes from a tag except the attributes specified, correction of cool routine from joris878 (who seems don't work) + example. When PHP will going to support this natively ? Sorry for my english. Hope everybody understand. --French-- Enlève des attributs d'une balise, sauf les attributs spécifiés dans un tableau. C'est une correction et un exemple de mise en oeuvre du code (très utile) posté par joris878 qui ne semblait pas fonctionner en l'état. Quand PHP supportera ceci de façon native ? ---------- <? function stripeentag($msg,$tag,$attr) { $lengthfirst = 0; while (strstr(substr($msg,$lengthfirst),"<$tag ")!="") { $imgstart = $lengthfirst + strpos(substr($msg,$lengthfirst), "<$tag "); $partafterwith = substr($msg,$imgstart); $img = substr($partafterwith,0,strpos($partafterwith,">")+1); $img = str_replace(" =","=",$msg); $out = "<$tag"; for($i=0; $i <= (count($attr) - 1 );$i++) { $long_val = strpos($img," ",strpos($img,$attr[$i]."=")) - (strpos($img,$attr[$i]."=") + strlen($attr[$i]) + 1) ; $val = substr($img, strpos($img,$attr[$i]."=") + strlen($attr[$i]) + 1,$long_val); if(strlen($val)>0) $attr[$i] = " ".$attr[$i]."=".$val; else $attr[$i] = ""; $out .= $attr[$i]; } $out .= ">"; $partafter = substr($partafterwith,strpos($partafterwith,">")+1); $msg = substr($msg,0,$imgstart).$out.$partafter; $lengthfirst = $imgstart+3; } return $msg; } $message = "<font size=\"10\" face=\"tahoma\" color=\"#DD0000\" >salut</font>" ; //on ne garde que la couleur //we want only "color" attribute $message = stripeentag($message,"font",array("color")); echo $message ; ?>

« previous php.notes (#154744) next »