本篇文章給大家分享的是有關PHP中htmlentities和htmlspecialchars有什么區別,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
htmlentities跟htmlspecialchars都是用于將字符串的特殊字符中轉成HTML字符實體。只是htmlspecialcharsl轉義的特殊字符集只有5個,
'&' (ampersand) => '&'
'"' (double quote) => '"' when ENT_NOQUOTES is not set.
''' (single quote) => ''' only when ENT_QUOTES is set.
'<' (less than) => '<'
'>' (greater than) => '>'
而htmlentities轉義的特殊字符集是完整的HTML字符實體集。
對于這兩個函數,php手冊上都是英文做的解釋,其中在htmlentities函數的說明部分有這么一段英文:
This function is identical(完全相同的) to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents(HTML
character entity equivalents譯為HTML字符實體等價物) are translated into these entities.
從這句話中我們也可以看出來這兩個函數雖然基本功能差不多,但是還是有細微的差別在里面的。再仔細看htmlspecialchars函數里面的一段話:
Certain characters(其實就是'&', 雙引號(沒有設置ENT_NOQUOTES時才會轉義), 單引號(當且僅當設置ENT_QUOTES才會轉義), '<', '>') have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function
returns a string with some of these conversions made; the translations made are those most useful for everyday web programming. If you require all HTML character entities to be translated, use htmlentities() instead.
注:使用這兩個函數時最后指明編碼格式哦。
以上就是PHP中htmlentities和htmlspecialchars有什么區別,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。