示例示例PHP 过滤 HTML 标签的方法有很多,下面介绍两种常用的方法:使用 strip_tags() 函数
PHP 过滤 HTML 标签的方法有很多,下面介绍两种常用的方法:
一、使用 strip_tags() 函数
strip_tags() 函数可以过滤掉字符串中的 HTML 标签,它可以接受第二个参数,表示允许保留的标签:
代码示例:
$str = "
Hello World!
This is a test.
";
echo strip_tags($str); // 输出:Hello World!This is a test.
echo strip_tags($str, '
'); // 输出:
Hello World!
This is a test.
二、使用 preg_replace() 函数
preg_replace() 函数可以使用正则表达式来替换字符串中的 HTML 标签:
代码示例:
$str = "
Hello World!
This is a test.
";
echo preg_replace("/<[^>]+>/","",$str); // 输出:Hello World!This is a test.