html_entity_decode() 不是工作 (PHP)


html_entity_decode() isn't work (PHP)

html_entity_decode() 返回 HTML 实体,而不是适用的字符。

<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
<?php
$code='&#97'; // It's a code of 'a' UTF-8 character.
$char = html_entity_decode($code, ENT_COMPAT, $encoding = 'UTF-8');
/*
Now $char must contains 'a' value, but it contains '&#97';
You can check this by following tests
*/
var_dump('&#97' === 'a');      // bool(false), of course.
var_dump('&#97' === $code);    // bool(true)
var_dump('&#97' === $char);    // bool(true) BUT MUST BE FALSE
var_dump($code === $char);     // bool(true) BUT MUST BE FALSE
// Or this:
echo str_replace('&', '', $char);    // it must print 'a', but it print '#97'

看起来这个函数在我的情况下什么都不做。怎么了?

&#97不是"'a'UTF-8字符的代码"。

然而,&#97;是。使用它,您的代码将按预期工作。