Home > Backend Development > PHP Tutorial > PHP converts html tags into legal html code program

PHP converts html tags into legal html code program

高洛峰
Release: 2016-11-29 15:51:46
Original
1247 people have browsed it

php函数代码如下:

function ihtmlspecialchars($string) 

 if(is_array($string)) 

 { 

  foreach($string as $key => $val) 

  { 

   $string[$key] = ihtmlspecialchars($val); 

  } 

 } else 

 {//开源代码phpfensi.com 

  $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 = '

echo ihtmlspecialchars( $str ); 

echo '
'; 

//echo <a href="http://www.php1.cn/"> 

echo htmlspecialchars ($str ); 

//print <a href="http://www.php1.cn/"> 

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


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