Detailed explanation of functions related to html tags in php

醉折花枝作酒筹
Release: 2023-03-08 21:02:02
Original
2283 people have browsed it

本文带大家了解一下PHP中与html标签相关函数(htmlspecialchars、strip_tags、addslashes)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

Detailed explanation of functions related to html tags in php

htmlspecialchars()函数

将特殊字元转成 HTML 格式,详细说本函数会转化以下字符:

    & (和) 转成 & 

    " (双引号) 转成 " 

    

<?php
$str=<<<start
<p style="color:red;font-size:28px;">单位确定为完全取得</p>
start;
echo $str;
echo &#39;<br />&#39;;
echo htmlspecialchars($str);
>
Copy after login

Detailed explanation of functions related to html tags in php

可以直观的看到上面那行的颜色,字体大小生效了,而下面一行的不一样,css样式并没有生效。

strip_tags()函数

从字符串中去除html和PHP标记。注意:本函数可去掉字串中包含的任何 HTML 及 PHP 的标记字串。若是字串的 HTML 及 PHP 标签原来就有错,例如少了大于的符号,则也会传回错误。

<p   style="max-width:90%">单位确定为完全取得</p>
start;
echo $str;
echo &#39;<br />&#39;;
echo strip_tags($str);
?>
Copy after login

其结果是:

<p style="color:color;font-size:28px;">单位确定为完全取得</p>
<br />单位确定为完全取得
Copy after login

通过这个实例可以看到,第二句话中的html全都被去除掉了。

addslashes()函数

addslashes是使用反斜线引用字符串。该函数一般都是数据库查询之前就需要处理的必要步骤,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。向字符串中的预定义字符添加反斜杠:这些字符是单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)。

<?php
$str = "Who&#39;s Bill Gates?";
echo $str . " This is not safe in a database query.<br>";
echo addslashes($str) . " This is safe in a database query.";
?>
Copy after login

得出的结果为:

Who&#39;s Bill Gates? This is not safe in a database query.
Who\&#39;s Bill Gates? This is safe in a database query.
Copy after login

推荐学习:《PHP视频教程

The above is the detailed content of Detailed explanation of functions related to html tags in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!