note 69478 modified in function.preg-replace by danbrown
| From: | danbrown@php.net | 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'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 �
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('~�*(\d+);~e', 'code2utf($1)', $str); ?>
--was--
Wasted several hours because of this:
$str='It'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 �
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('~�*(\d+);~e', 'code2utf($1)', $str);
http://php.net/manual/en/function.preg-replace.php