Home > Backend Development > PHP Tutorial > htmlspecialchars(),addslashes()详解_PHP

htmlspecialchars(),addslashes()详解_PHP

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:38:02
Original
1247 people have browsed it

默认你已经了解HTML字符实体

$html = "

";

1.htmlspecialchars($html)

作用:传入字符串$html,将$html中包含等HTML中预留的字符,转换成字符实体,返回转换后的字符串

应用场景:

    a.想在HTML中直接显示源码(等同右击=>查看源码效果),此时可以使用htmlspecialchars()对想输出的源码进行转义;

    b.文本过滤:在表单页,防止恶意注入,如在输入框中输入,此时使用htmlspecialchars()转义后,该脚本中的将会被转为字符实体,在php中不会执行.

2.addslashes($html)

作用:传入字符串$html,addslashes()会将$html中的单引,双引号全部转义

应用场景:

    a.数据库插入大段HTML字符串


示例代码:

<?php $html = "<div id='MH370'>MH370<div>";$hsc = htmlspecialchars($html);$ass = addslashes($html);var_dump($html);var_dump($hsc);var_dump($ass);?>  <p>返回结果:</p>  <p></p>  <pre name="code" class="sycode">string '<div id="MH370">MH370<div>' (length=26)string <span style="color:#cc0000;">'<div id='MH370'>MH370<div></span>' (length=38)string '<div id="\'MH370\'">MH370<div>' (length=28)  <br>  <br>  <p></p>  <br>  <p></p> </div>
</div>
</div>
</div>
Copy after login
Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template