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});)/', ‘&\\1′, str_replace(array(‘&', ‘”‘, ‘<', ‘>'), array(‘&', ‘"', ‘<', ‘>'), $string)); } return $string; }
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!