htmlentities
將所有適用的字符轉換為HTML實體-將字符轉換為HTML 轉義字符
htmlentities()
函數把字符轉換為HTML 實體。
提示:要把HTML 實體轉換回字符,請使用html_entity_decode()
函數。
提示:請使用get_html_translation_table()
函數來返回htmlentities()
使用的翻譯表。
把字符轉換為HTML 實體:
<?php $str = "<? W3S?h????>" ; echo htmlentities ( $str ) ; ?>
以上代碼的HTML 輸出如下(查看源代碼):
< ! DOCTYPE html > < html > < body > < ? W3S ?h°°|§ > < / body > < / html >
以上代碼的瀏覽器輸出:
<? W3S ? h ?? ? ?>
親自試一試
把字符轉換為HTML 實體:
<?php $str = "Bill & 'Steve'" ; echo htmlentities ( $str , ENT_COMPAT ) ; // 只轉換雙引號 echo "<br>" ; echo htmlentities ( $str , ENT_QUOTES ) ; // 轉換雙引號和單引號 echo "<br>" ; echo htmlentities ( $str , ENT_NOQUOTES ) ; // 不轉換任何引號 ?>
以上代碼的HTML 輸出如下(查看源代碼):
< ! DOCTYPE html > < html > < body > Bill & 'Steve' < br > Bill & & #039;Tarzan'<br> Bill & 'Steve' < / body > < / html >
以上代碼的瀏覽器輸出:
Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'
親自試一試
通過使用西歐字符集,把一些字符轉換為HTML 實體:
<?php $str = "My name is ?yvind ?sane. I'm Norwegian." ; echo htmlentities ( $str , ENT_QUOTES , "ISO-8859-1" ) ; // Will only convert double quotes (not single quotes), and uses the character-set Western European ?>
以上代碼的HTML 輸出如下(查看源代碼):
< ! DOCTYPE html > < html > < body > My name is ?yvind ?sane . I 'm Norwegian . < / body > < / html >
以上代碼的瀏覽器輸出:
My name is ?yvind ?sane. I'm Norwegian.
親自試一試
htmlentities ( string , flags , character - set , double_encode )
參數 | 描述 |
---|---|
string | 必需。規定要轉換的字符串。 |
flags |
可選。規定如何處理引號、無效的編碼以及使用哪種文檔類型。 可用的引號類型:
無效的編碼:
規定使用的文檔類型的附加flags:
|
character-set |
可選。一個規定了要使用的字符集的字符串。 允許的值:
註釋:在PHP 5.4 之前的版本,無法被識別的字符集將被忽略並由ISO-8859-1 替代。自PHP 5.4 起,無法被識別的字符集將被忽略並由UTF-8 替代。 |
double_encode |
可選。布爾值,規定是否編碼已存在的HTML 實體。
|