note 79755 added to function.preg-replace
| From: | admin | Date: | Tue, 11 Dec 2007 14:49:55 +0000 |
| Subject: | note 79755 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-133367@lists.php.net to get a copy of this message | ||
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 & 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", "&", $text); // This matches and
replaces all amperands which are not written as &
$text=preg_replace("/&(#\d+);|&(\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 ...
----
Server IP: 69.147.83.197
Probable Submitter: 89.122.151.77
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/79755
Del: integrated -- https://master.php.net/note/delete/79755/integrated
Del: useless -- https://master.php.net/note/delete/79755/useless
Del: bad code -- https://master.php.net/note/delete/79755/bad+code
Del: spam -- https://master.php.net/note/delete/79755/spam
Del: non-english -- https://master.php.net/note/delete/79755/non-english
Del: in docs -- https://master.php.net/note/delete/79755/in+docs
Del: other reasons-- https://master.php.net/note/delete/79755
Reject -- https://master.php.net/note/reject/79755
Search -- https://master.php.net/manage/user-notes.php