想找一个可以在PHP中使用的函数,可以将字符串中的HTML标记去除。
发布时间:2023-08-20 03:12:41
在PHP中,可以使用strip_tags()函数将字符串中的HTML标记去除。
strip_tags()函数的语法如下:
string strip_tags ( string $str [, string $allowable_tags ] )
参数说明:
- $str: 要处理的字符串。
- $allowable_tags(可选): 允许保留的标记,可以是一个标记字符串,也可以是多个标记组成的数组。例如,<p>标记可以保留成文本,可以使用<p>作为$allowable_tags参数。
示例使用:
$html = '<h1>Hello, World!</h1><p>This is a <b>sample</b> text.</p>'; // 移除所有HTML标记 $cleanText = strip_tags($html); echo $cleanText; // 输出结果: Hello, World!This is a sample text. // 保留<p>标记 $cleanText = strip_tags($html, '<p>'); echo $cleanText; // 输出结果: <p>This is a sample text.</p>
strip_tags()函数能够简单快速地去除HTML标记,但是它也可能会去除掉一些有用的文本内容。所以在使用时,请根据实际情况选择是否保留某些标记或者使用其他更复杂的处理方式。
