您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP如何将HTML字符转义?

2024/5/29 6:32:57发布47次查看
php如何将html字符转义?
在php中可以使用“htmlspecialchars()”函数将html字符转义,该函数的作用是将特殊字符转换为html实体,其用法为“htmlspecialchars($string)”,其参数“$string”代表待转换的字符。
推荐视频教程:《php编程从入门到精通(学习路线)》
示例
<?php$new = htmlspecialchars("<a href='test'>test</a>", ent_quotes);echo $new; // &lt;a href=&#039;test&#039;&gt;test&lt;/a&gt;?>
<?phpfunction fixtags($text){$text = htmlspecialchars($text);$text = preg_replace("/=/", "=\"\"", $text);$text = preg_replace("/&quot;/", "&quot;\"", $text);$tags = "/&lt;(\/|)(\w*)(\ |)(\w*)([\\\=]*)(?|(\")\"&quot;\"|)(?|(.*)?&quot;(\")|)([\ ]?)(\/|)&gt;/i";$replacement = "<$1$2$3$4$5$6$7$8$9$10>";$text = preg_replace($tags, $replacement, $text);$text = preg_replace("/=\"\"/", "=", $text);return $text;}?>an example:<?php$string = "this is smaller < than this<br />this is greater > than this<br />this is the same = as this<br /><a href=\"http://www.example.com/example.php?test=test\">this is a link</a><br /><b>bold</b> <i>italic</i> etc...";echo fixtags($string);?>
推荐教程:《php》
以上就是php如何将html字符转义?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product