問題描述
我讀過專業 WordPress,它説:
esc_htmlfunction is used for scrubbing data that contains HTML. This function encodes special characters into their HTML entities
esc_attrfunction is used for escaping HTML attributes
esc_url. This function should be used to scrub the URL for illegal characters. Even though the href is technically an HTML attribute
這些有什麼區別?
如果我有
<script>alert('hello world!');</script>this is some content
將所有< > 轉換為< >?網址會像%xxx 那樣嗎?
最佳解決方案
esc_html 和 esc_attr 是 near-identical,唯一的區別是輸出通過不同命名的過濾器 (分別為 esc_html 和 attribute_escape) 。
esc_url 更復雜和更具體,它處理不能在 URL 和允許的協議中的字符 (列表可以作為第二個參數傳遞) 。如果它不存在,它也將使用 http://協議來預置輸入 (並且鏈接不是相對的) 。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。