htmlspecialchars() 函數把預定義的字符轉換為 HTML 實體。
預定義的字符是:
- & (和號)成為 &
- " (雙引號)成為 $quot;
- ' (單引號)成為 '
- < (小于)成為 <
- > (大于)成為 >
<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
?>
上面的代碼html輸出后:
<!DOCTYPE html>
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>
瀏覽器輸出:
This is some <b>bold</b> text.