問題描述

我讀過專業 WordPress,它說:

esc_html function is used for scrubbing data that contains HTML. This function encodes special characters into their HTML entities

esc_attr function 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

將所有< > 轉換為&lt; &gt;?網址會像%xxx 那樣嗎?

最佳解決方案

esc_htmlesc_attr 是 near-identical,唯一的區別是輸出透過不同命名的過濾器 (分別為 esc_htmlattribute_escape) 。

esc_url 更復雜和更具體,它處理不能在 URL 和允許的協議中的字元 (列表可以作為第二個引數傳遞) 。如果它不存在,它也將使用 http://協議來預置輸入 (並且連結不是相對的) 。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。