html_entity_decode
將HTML實體轉換為相應的字符
html_entity_decode()
函數把HTML 實體轉換為字符。
html_entity_decode()
函數是htmlentities()
函數的反函數。
把HTML 實體轉換為字符:
<?php $str = "<? W3S?h°°|§>" ; echo html_entity_decode ( $str ) ; ?>
以上代碼的HTML 輸出如下(查看源代碼):
<! DOCTYPE html > < html > < body > <? W3S ? h ?? ? ?> </ body > </ html >
以上代碼的瀏覽器輸出:
<? W3S ? h ?? ? ?>
把HTML 實體轉換為字符:
<?php $str = "Bill & 'Steve'" ; echo html_entity_decode ( $str , ENT_COMPAT ) ; // 只轉換雙引號 echo "<br>" ; echo html_entity_decode ( $str , ENT_QUOTES ) ; // 轉換雙引號和單引號 echo "<br>" ; echo html_entity_decode ( $str , ENT_NOQUOTES ) ; // 不轉換任何引號 ?>
以上代碼的HTML 輸出(查看源代碼):
< ! DOCTYPE html > < html > < body > Bill & & #039;Steve'<br> Bill & 'Steve' < br > Bill & & #039;Steve' < / body > < / html >
以上代碼的瀏覽器輸出:
Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'
通過使用西歐字符集,把HTML 實體轉換為字符:
<?php $str = "My name is ?yvind ?sane. I'm Norwegian." ; echo html_entity_decode ( $str , ENT_QUOTES , "ISO-8859-1" ) ; ?>
以上代碼的HTML 輸出(查看源代碼):
< ! DOCTYPE html > < html > < body > My name is ? yvind ? sane . I 'm Norwegian . < / body > < / html >
以上代碼的瀏覽器輸出:
My name is ?yvind ?sane. I'm Norwegian.
html_entity_decode ( string , flags , character - set )
參數 | 描述 |
---|---|
string | 必需。規定要解碼的字符串。 |
flags |
可選。規定如何處理引號以及使用哪種文檔類型。 可用的引號類型:
規定所使用文檔類型的附加flags:
|
character-set |
可選。字符串值,規定要使用的字符集。 允許的值:
註釋:在PHP 5.4 之前的版本,無法被識別的字符集將被忽略並由ISO-8859-1 替代。自PHP 5.4 起,無法被識別的字符集將被忽略並由UTF-8 替代。 |