2014-12-13 235 views
-2

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

<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'

貌似这个函数什么也不做在我的情况。 有什么问题?

+0

您的字符引用缺少分隔';':'a'。 – Gumbo 2014-12-13 12:27:47

+0

PHP不是Python,它没有命名参数。 '$ encoding ='UTF-8''可能不会做你认为它的作用。函数调用应该是简单的'html_entity_decode($ code,ENT_COMPAT,'UTF-8')'。 – deceze 2014-12-13 12:33:00

回答

0

&#97不是“UTF-8字符的代码”。

但是,&#97;是。使用它,你的代码将按预期工作。