
PHP如何转义HTML字符?
在PHP中可以通过使用“htmlentities()”函数转义HTML字符,该函数的作用是将字符转换为HTML转义字符,使用方法只需将HTML传入该函数,其返回值是转义后的HTML字符。
语法
1 | htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get ( "default_charset" ) [, bool $double_encode = true ]]] ) : string
|
登入後複製
使用示例
1 2 3 4 5 6 7 8 9 | <?php
$str = "A 'quote' is <b>bold</b>" ;
echo htmlentities( $str );
echo htmlentities( $str , ENT_QUOTES);
?>
|
登入後複製
1 2 3 4 5 6 7 8 9 | <?php
$str = "\x8F!!!" ;
echo htmlentities( $str , ENT_QUOTES, "UTF-8" );
echo htmlentities( $str , ENT_QUOTES | ENT_IGNORE, "UTF-8" );
?>
|
登入後複製
推荐教程:《PHP》
以上是PHP如何轉義HTML字元?的詳細內容。更多資訊請關注PHP中文網其他相關文章!