php如何转换html标签,使其能在浏览器中正常显示?
在编程中需要把提交的内容转化成html标签,这样才能在浏览器中正常显示。比如要把'<'转化成'<',把空格' '转换成' '等。
其实php已经有了这样的函数,那就是:html_entity_decode
<?php
$new = htmlspecialchars("<a href='test'>Test</a>");
echo $new; //out <a href='test'>Test</a>
echo html_entity_decode($new); //out <a href='test'>Test</a>
?>
PHP压缩html
function compress_html($string) {
$string = str_replace("\r\n", '', $string); //清除换行符
$string = str_replace("\n", '', $string); //清除换行符
$string = str_replace("\t", '', $string); //清除制表符
$pattern = array(
"/> *([^ ]*) *</", //去掉注释标记
"/[\s]+/",
"/<!--[\\w\\W\r\\n]*?-->/",
"/\" /",
"/ \"/",
"'/\*[^*]*\*/'"
);
$replace = array(
">\\1<",
" ",
"",
"\"",
"\"",
""
);
return preg_replace($pattern, $replace, $string);
}
过滤HTML属性
$html = preg_replace("/<([a-zA-Z]+)[^>]*>/","<\\1>",$html);