【发布时间】:2014-12-13 12:22:41
【问题描述】:
html_entity_decode() 返回 HTML 实体,而不是适用的字符。
<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
<?php
$code='a'; // 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 'a';
You can check this by following tests
*/
var_dump('a' === 'a'); // bool(false), of course.
var_dump('a' === $code); // bool(true)
var_dump('a' === $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;。 -
PHP 不是 Python,它没有命名参数。
$encoding = 'UTF-8'可能不会像您认为的那样做。函数调用应该是简单的html_entity_decode($code, ENT_COMPAT, 'UTF-8')。