note 69478 deleted from function.preg-replace by crell

From: Date: Fri, 01 Jul 2022 19:51:56 +0000
Subject: note 69478 deleted from function.preg-replace by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218867@lists.php.net to get a copy of this message
Note Submitter: Alexey Lebedev ---- 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); ?>

« previous php.notes (#218867) next »