note 69478 modified in function.preg-replace by danbrown

From: Date: Fri, 28 Nov 2008 19:55:30 +0000
Subject: note 69478 modified in function.preg-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147156@lists.php.net to get a copy of this message
Wasted several hours because of this: <?php $str='It&#039;s a string with HTML entities'; preg_replace('~&#(\d+);~e', 'code2utf($1)', $str); ?> This code must convert numeric html entities to utf8. And it does with a little exception. It treats wrong codes starting with &#0 The reason is that code2utf will be called with leading zero, exactly what the pattern matches - code2utf(039). And it does matter! PHP treats 039 as octal number. Try <?php print(011); ?> Solution: <?php preg_replace('~&#0*(\d+);~e', 'code2utf($1)', $str); ?> --was-- Wasted several hours because of this: $str='It&#039;s a string with HTML entities'; preg_replace('~&#(\d+);~e', 'code2utf($1)', $str); This code must convert numeric html entities to utf8. And it does with a little exception. It treats wrong codes starting with &#0 The reason is that code2utf will be called with leading zero, exactly what the pattern matches - code2utf(039). And it does matter! PHP treats 039 as octal number. Try print(011); Solution: preg_replace('~&#0*(\d+);~e', 'code2utf($1)', $str); http://php.net/manual/en/function.preg-replace.php

« previous php.notes (#147156) next »