php中htmlspecialchars,htmlentities用法_PHP教程

來源:互聯網
上載者:User
在php中htmlspecialchars, 將特殊字元轉成 HTML 格式,而htmlentities,將所有的字元都轉成 HTML 字串 了,下面我來分別簡單的介紹。

htmlentities用法



$str = "John & 'Adams'";
echo htmlentities($str, ENT_COMPAT);
echo "
";
echo htmlentities($str, ENT_QUOTES);
echo "
";
echo htmlentities($str, ENT_NOQUOTES);
?>

John & 'Adams'
John & 'Adams'
John & 'Adams'

瀏覽器輸出:
代碼如下 複製代碼

htmlspecialchars用法

& (和) 轉成 &
" (雙引號) 轉成 "
< (小於) 轉成 <
> (大於) 轉成 >



$str = "John & 'Adams'";
echo htmlspecialchars($str, ENT_COMPAT);
echo "
";
echo htmlspecialchars($str, ENT_QUOTES);
echo "
";
echo htmlspecialchars($str, ENT_NOQUOTES);
?>

代碼如下 複製代碼

他們的區別

這兩個函數的功能都是轉換字元為HTML字元編碼,特別是url和代碼字串。防止字元標記被瀏覽器執行。使用中文時沒什麼區別,但htmlentities會格式化中文字元使得中文輸入是亂碼

htmlentities轉換所有的html標記,htmlspecialchars只格式化& ' " < 和 > 這幾個特殊符號

代碼如下 複製代碼

$str = '測試頁面';

echo 'htmlentities指定GB2312編碼:'.htmlentities($str,ENT_COMPAT,"GB2312").'';

echo 'htmlentities未指定編碼:'.htmlentities($str).'';

$str = '測試頁面';

echo htmlspecialchars($str).'';

效果:

代碼如下 複製代碼

htmlentities指定GB2312編碼:測試頁面

htmlentities未指定編碼:²âÊÔÒ³Ãæ

測試頁面


http://www.bkjia.com/PHPjc/445287.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445287.htmlTechArticle在php中htmlspecialchars, 將特殊字元轉成 HTML 格式,而htmlentities,將所有的字元都轉成 HTML 字串 了,下面我來分別簡單的介紹。 htmlentities用法...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.