note 79758 modified in function.preg-replace by danbrown

From: Date: Fri, 28 Nov 2008 19:49:52 +0000
Subject: note 79758 modified in function.preg-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147145@lists.php.net to get a copy of this message
<?php function repl_amp($text) { $text=preg_replace("/&(?!amp;)/i", "&", $text); $text=preg_replace("/&#(\d+);/i", "&#$1;", $text); // For numeric entities $text=preg_replace("/&(\w+);/i", "&$1;", $text); // For literal entities return $text; } ?> The RegEx Tester says that the first expression is OK, but when testing with various entities, some of them came out broken. I'd tried to use only 2 preg_replace(); calls instead of three by using the alternative branch from the pattern syntax - which didn't came out well. Sorry for the previous error, and I still hope that someone can find a better alternative. --was-- Actually I made a mistake in my previous post. In order to make the function more effective ... I broke it. The original which works (really) looks like this: <?php function repl_amp($text) { $text=preg_replace("/&(?!amp;)/i", "&amp;", $text); $text=preg_replace("/&amp;#(\d+);/i", "&#$1;", $text); // For numeric entities $text=preg_replace("/&amp;(\w+);/i", "&$1;", $text); // For literal entities return $text; } ?> The RegEx Tester says that the first expression is OK, but when testing with various entities, some of them came out broken. I'd tried to use only 2 preg_replace(); calls instead of three by using the alternative branch from the pattern syntax - which didn't came out well. Sorry for the previous error, and I still hope that someone can find a better alternative. http://php.net/manual/en/function.preg-replace.php

« previous php.notes (#147145) next »