In-depth analysis of php htmlspecialchars() and shtmlspecialchars() functions_PHP tutorial

WBOY
Release: 2016-07-21 15:09:24
Original
932 people have browsed it

Definition and usage
The htmlspecialchars() function converts some predefined characters into HTML entities.

The predefined characters for

are:
•& (ampersand) becomes &
•" (double quote) becomes "
•' (single quote) Quotes) becomes '
•< (less than) becomes <
•> (greater than) becomes>
Syntax
htmlspecialchars(string,quotestyle, character-set)

Parameters Description
string Required. Specifies the string to be converted.
quotestyle
参数 描述
string 必需。规定要转换的字符串。
quotestyle

可选。规定如何编码单引号和双引号。

  • ENT_COMPAT - 默认。仅编码双引号。
  • ENT_QUOTES - 编码双引号和单引号。
  • ENT_NOQUOTES - 不编码任何引号。
character-set

可选。字符串值,规定要使用的字符集。

  • ISO-8859-1 - 默认。西欧。
  • ISO-8859-15 - 西欧(增加 Euro 符号以及法语、芬兰语字母)。
  • UTF-8 - ASCII 兼容多字节 8 比特 Unicode
  • cp866 - DOS 专用 Cyrillic 字符集
  • cp1251 - Windows 专用 Cyrillic 字符集
  • cp1252 - Windows 专用西欧字符集
  • KOI8-R - 俄语
  • GB2312 - 简体中文,国家标准字符集
  • BIG5 - 繁体中文
  • BIG5-HKSCS - Big5 香港扩展
  • Shift_JIS - 日语
  • EUC-JP - 日语
Optional. Specifies how single and double quotes are encoded.
  • ENT_COMPAT - Default. Only double quotes are encoded.
  • ENT_QUOTES - Encodes double and single quotes.
  • ENT_NOQUOTES - Do not encode any quotes.
character-set Optional. String value specifying the character set to use.
  • ISO-8859-1 - Default. Western Europe.
  • ISO-8859-15 - Western Europe (adds Euro symbol and French and Finnish letters).
  • UTF-8 - ASCII compatible multi-byte 8-bit Unicode
  • cp866 - DOS-specific Cyrillic character set
  • cp1251 - Cyrillic character set for Windows
  • cp1252 - Windows-specific Western European character set
  • KOI8-R - Russian
  • GB2312 - Simplified Chinese, national standard character set
  • BIG5 - Traditional Chinese
  • BIG5-HKSCS - Big5 Hong Kong Extension
  • Shift_JIS - Japanese
  • EUC-JP - Japanese

Tips and Notes

Tip:
Unrecognized character sets will be ignored and replaced by ISO-8859-1. Example
Copy code

The code is as follows:




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


Browser output:
Copy code

The code is as follows:


John & ' Adams'
John & 'Adams'
John & 'Adams'
If you view the source code in a browser, you'll see this HTML:
Copy code

The code is as follows:



                                                                                                                                                                          John & 'Adams'
John & 'Adams'


============== ================================================== ========shtmlspecialchars() function is just the opposite

http://www.bkjia.com/PHPjc/327307.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327307.htmlTechArticle
Definition and usage The htmlspecialchars() function converts some predefined characters into HTML entities. The predefined characters are: amp; " (double quote) becomes ' (single quote) becomes (less than...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!