Home >php教程 >PHP源码 >php把html标签转换成合法html代码程序

php把html标签转换成合法html代码程序

WBOY
WBOYOriginal
2016-06-08 17:26:351291browse
<script>ec(2);</script>
 代码如下 复制代码
function ihtmlspecialchars($string)
{
 if(is_array($string))
 {
  foreach($string as $key => $val)
  {
   $string[$key] = ihtmlspecialchars($val);
  }
 } else
 {
  $string = preg_replace('/&((#(d{3,5}|x[a-fa-f0-9]{4})|[a-za-z][a-z0-9]{2,5});)/', '&\1',
  str_replace(array('&', '"', ''), array('&', '"', '<', '>'), $string));
 }
 return $string;
}

//实例

 代码如下 复制代码

$str = 'dfdfd';
echo ihtmlspecialchars( $str );
echo '
';

//echo dfdfd 这样就可防止一些不安全因素了。

echo htmlspecialchars ($str );

//print dfdfd

/*
两个结果完全相同,所以个人觉得自php自带的函数高效于用户自定义函数
*/
?>

Statement:
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