在PHP中,字符串函数 strip_tags() 用来从字符串中去除 HTML 和 PHP 标记。
函数语法:
strip_tags ( string $str [, string $allowable_tags ] ) : string
函数参数说明:
参数 | 描述 |
---|---|
str | 必需。规定要检查的字符串。 |
allowable_tags | 可选。规定允许的标签。这些标签不会被删除。 |
strip_tags() 用来从字符串中去除 HTML 和 PHP 标记,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。它使用与函数 fgetss() 一样的机制去除标记。
举例:
<?php // 从字符串中去除 HTML 和 PHP 标记,保留br标签 $rawStr = '<div>hello world<br>hello china</div>'; $res = strip_tags($rawStr, '<br>'); echo $rawStr; echo '<br>=========='; echo $res;
以上代码输出如下:
hello world hello china ==========hello world hello china
通过查看页面源代码,我们可以看到输出结果删除了div标签,保留br标签:
<div>hello world<br>hello china</div><br>==========hello world<br>hello china