PHP若何将HTML字符本义?
正在PHP中能够应用“htmlspecialchars()”函数将HTML字符本义,该函数的作用是将非凡字符转换为HTML实体,其用法为“htmlspecialchars($string)”,其参数“$string”代表待转换的字符。
保举视频教程:《PHP编程从入门到通晓(学习道路)》
示例
<?php $new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES); echo $new; // <a href='test'>Test</a> ?>
<?php function fixtags($text){ $text = htmlspecialchars($text); $text = preg_replace("/=/", "=\"\"", $text); $text = preg_replace("/"/", ""\"", $text); $tags = "/<(\/|)(\w*)(\ |)(\w*)([\\\=]*)(?|(\")\""\"|)(?|(.*)?"(\")|)([\ ]?)(\/|)>/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字符本义?的具体内容,更多请存眷资源魔其它相干文章!
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!