2014-12-13 240 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;是。使用它,你的代碼將按預期工作。