search
Homephp教程php手册php htmlspecialchars()与shtmlspecialchars()函数的深入分析

php htmlspecialchars()与shtmlspecialchars()函数的深入分析

Jun 13, 2016 am 11:51 AM
hhtmlspecialcharsphpandfunctionIn-depth analysisusageof

定义和用法
htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。

预定义的字符是:
•& (和号) 成为 &
•" (双引号) 成为 "
•' (单引号) 成为 '
••> (大于) 成为 >
语法
htmlspecialchars(string,quotestyle,character-set)

参数 描述
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 - 日语

提示和注释
提示:
无法被识别的字符集将被忽略,并由 ISO-8859-1 代替。
例子

复制代码 代码如下:



   


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


浏览器输出:

复制代码 代码如下:


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


如果在浏览器中查看源代码,会看到这些 HTML:

复制代码 代码如下:



    


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


=======================================================================
shtmlspecialchars() 函数正好相反
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.