>  기사  >  백엔드 개발  >  php 특수문자 필터링

php 특수문자 필터링

巴扎黑
巴扎黑원래의
2016-11-09 11:52:361307검색

1. htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体
函数原型:htmlspecialchars(string,quotestyle,character-set)
预定义的字符是:
    & (和号) 成为 &
    ” (双引号) 成为 "
    ‘ (单引号) 成为 '
    < (小于) 成为 <
    > (大于) 成为 >
<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
?>
以上代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html>
<html> <body> This is some <b>bold</b> text. </body> </html>
以上代码的浏览器输出:
This is some <b>bold</b> text.
2. htmlspecialchars_decode() 函数把一些预定义的 HTML 实体转换为字符(和htmlspecialchars相反)
函数原型:htmlspecialchars_decode(string,quotestyle)
<?php $str = "This is some <b>bold</b> text."; echo htmlspecialchars_decode($str); ?>
以上代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html> <html> <body> This is some <b>bold</b> text. </body> </html>
以上代码的浏览器输出:
This is some bold text.


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.