php htmlentities() 函数使用实例,htmlentities实例
php htmlentities() 函数把字符转换为 HTML 实体,本文章向码农介绍php htmlentities() 函数基本使用方法和实例介绍,需要的码农可以参考一下。
定义和用法
htmlentities() 函数把字符转换为 HTML 实体。
提示:要把 HTML 实体转换回字符,请使用 html_entity_decode() 函数。
提示:请使用 get_html_translation_table() 函数来返回 htmlentities() 使用的翻译表。
语法
htmlentities(string,flags,character-set,double_encode)
参数 | 描述 |
---|---|
string | 必需。规定要转换的字符串。 |
flags |
可选。规定如何处理引号、无效的编码以及使用哪种文档类型。 可用的引号类型:
无效的编码:
规定使用的文档类型的附加 flags:
|
character-set |
可选。一个规定了要使用的字符集的字符串。 允许的值:
注释:在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。 |
double_encode |
可选。布尔值,规定是否编码已存在的 HTML 实体。
|
技术细节
返回值: |
返回被转换的字符串。 如果 string 包含无效的编码,则返回一个空的字符串,除非设置了 ENT_IGNORE 或者 ENT_SUBSTITUTE 标志。 |
PHP 版本: | 4+ |
更新日志: |
在 PHP 5 中,character-set 参数的默认值改为 UTF-8。 在 PHP 5.4 中,新增了:ENT_SUBSTITUTE、ENT_DISALLOWED、ENT_HTML401、ENT_HTML5、ENT_XML1 和 ENT_XHTML。 在 PHP 5.3 中,新增了 ENT_IGNORE。 在 PHP 5.2.3 中,新增了 double_encode 参数。 在 PHP 4.1 中,新增了 character-set 参数。 |
实例
例子 1
把字符转换为 HTML 实体:
<?php $str = "Bill & 'Steve'"; echo htmlentities($str, ENT_COMPAT); // 只转换双引号 echo "<br>"; echo htmlentities($str, ENT_QUOTES); // 转换双引号和单引号 echo "<br>"; echo htmlentities($str, ENT_NOQUOTES); // 不转换任何引号 ?>
在线运行
以上代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html> <html> <body> Bill & 'Steve'<br> Bill & 'Tarzan'<br> Bill & 'Steve' </body> </html>
以上代码的浏览器输出:
Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'
例子 2
通过使用西欧字符集,把一些字符转换为 HTML 实体:
<?php $str = "My name is ?yvind ?sane. I'm Norwegian."; echo htmlentities($str, ENT_QUOTES, "ISO-8859-1"); // Will only convert double quotes (not single quotes), and uses the character-set Western European ?>
以上代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html> <html> <body> My name is Øyvind Åsane. I'm Norwegian. </body> </html>
以上代码的浏览器输出:
My name is ?yvind ?sane. I'm Norwegian.
原文地址:http://www.manongjc.com/article/794.html
PHP相关学习资料:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools