Home > Backend Development > PHP Tutorial > Detailed explanation of php htmlspecialchars example code

Detailed explanation of php htmlspecialchars example code

怪我咯
Release: 2023-03-13 22:44:02
Original
1934 people have browsed it

htmlspecialchars() 函数把预定义的字符转换为 HTML 实体。

预定义的字符是:

  • & (和号)成为 &

  • " (双引号)成为 "

  • ' (单引号)成为 '

  • < (小于)成为 <

  • > (大于)成为 >

此函数返回被转换的字符串

示例代码如下:

//取消HTML代码 
function shtmlspecialchars($string) { 
if(is_array($string)) { 
foreach($string as $key => $val) { 
$string[$key] = shtmlspecialchars($val); 
} 
} else { 
$string = preg_replace(‘/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/&#39;, ‘&\\1′, 
str_replace(array(‘&&#39;, ‘”‘, ‘<&#39;, ‘>&#39;), array(‘&&#39;, ‘"&#39;, ‘<&#39;, ‘>&#39;), $string)); 
} 
return $string; 
}
Copy after login

The above is the detailed content of Detailed explanation of php htmlspecialchars example code. For more information, please follow other related articles on the PHP Chinese website!

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