note 79755 deleted from function.preg-replace by felipe

From: Date: Sat, 15 Dec 2007 15:32:55 +0000
Subject: note 79755 deleted from function.preg-replace by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-133574@lists.php.net to get a copy of this message
Note Submitter: admin[a-t]saltwaterc[d0t]net ---- I've been looking for a long time in order to find a solution for ampersands which were introduced as & in a database, which should be posted as &amp; in the generated page without altering the existing HTML tags which should be posted as HTML tags (so htmlentities(); or htmlspecialchars(); won't do it), nor altering the existing HTML entities. After hours of Goggling because my RegEx knowledge is less than average ... I wrote my own solution which works quite fine ... <?php function repl_amp($text) { $text=preg_replace("/&(?!amp;)/i", "&amp;", $text); // This matches and replaces all amperands which are not written as &amp; $text=preg_replace("/&amp;(#\d+);|&amp;(\w+);/i", "&$1;", $text); // This fixes the broken numeric/literal HTML Entities which might be broken by the previous preg_replace(); call, yes, I couldn't avoid breaking the entities return $text; // Of course ... ye output } ?> If someone can do a better solution ... please go ahead ...

« previous php.notes (#133574) next »