在php中可以通过使用“htmlentities()”函数转义html字符,该函数的作用是将字符转换为html转义字符,使用方法只需将html传入该函数,其返回值是转义后的html字符。
语法
htmlentities ( string $string [, int $flags = ent_compat | ent_html401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] ) : string
使用示例
<?php$str = "a 'quote' is <b>bold</b>";// 输出: a 'quote' is <b>bold</b>echo htmlentities($str);// 输出: a 'quote' is <b>bold</b>echo htmlentities($str, ent_quotes);?>
<?php$str = "\x8f!!!";// 输出空 stringecho htmlentities($str, ent_quotes, "utf-8");// 输出 "!!!"echo htmlentities($str, ent_quotes | ent_ignore, "utf-8");?>
推荐教程:《php》
以上就是php如何转义html字符?的详细内容。
