How to use regular replacement to clear all HTML tags in a string?

慕斯
Release: 2023-03-10 13:54:01
Original
2161 people have browsed it

上篇文章给大家介绍了《PHP中我们如何自定义匹配手机号的正则表达式?(附代码)》,本文继续给大家介绍如何使用正则替换的方式实现清除字符串中所有的HTML标签?(详细介绍)

How to use regular replacement to clear all HTML tags in a string?

如何编写一个函数,使用正则替换的方式能够实现清除字符串中所有的HTML标签?

如果我们想要去清楚字符串HTML的标签去要把HTML的标签全部列出来,首先我们先把字符串定义下来,总的来说我们HTML的标签主要有两种情况,一种是双标签以什么开始,以什么结束,另外一种就是单标签(或者是input)结束,一般情况下,我们不会把这个标签给删掉,比如说写一个(input)标签的文本域,如果我们把标签删掉了,文本域就没了,因此我们不会直接这样做,我们会把相对应的标签转化为实体,假如,我们直接输出(echo)$str;我们运行结果,空白栏外是front标签,里面是普通的文本域,(代码结果如下所示)

<?php
/**** ***清楚字符串HTML标签* *******/
$str =&#39;<font>高考加油</font><input type="text"/>&#39;;
echo $str;
?>
Copy after login

代码结果如下所示

How to use regular replacement to clear all HTML tags in a string?

以上这种情况,如果我们要是清除,是不行的,因此,我们会对上述代码进行替换,首先,我们先定义一个函数,function demo(),给到一个字符串,紧接着我们定义正则,$pattern,然后我们需要使用到两个符号,第一(//S),当我们找到之后,我们进行替换,$replace,一个是(<)另一个是(>),最后我们直接(return)他们的结果。输出(echo)demo($str),最后我们得到的结果就是原样输出,

代码如下:

<?php
/**** ***清楚字符串HTML标签* *******/
$str =&#39;<font>高考加油</font><input type="text"/>&#39;;
echo $str;


function demo($str){
    //定义正则
    $pattern = array(
    &#39;/</S&#39;,
    &#39;/>/S&#39;
    );
    $replace = array(&#39;<&#39;,&#39;>&#39; );
    return preg_replace($pattern, $replace, $str);
}
    echo &#39;<hr/>&#39; ;
    echo demo($str);
?>
Copy after login

代码结果如下所示;

How to use regular replacement to clear all HTML tags in a string?

其实一样,我不使用这个函数,系统中也有其他的函数帮我们实现;

例如:

  • (htmlentities)---将字符串转换为HTML转义字符。

  • (htmlspecialchars)---特殊字符转换为HTML实体。

现在我们用(htmlspecialchars)函数进行编码,然后进行代码演示:

代码如下:

<?php
/**** ***清楚字符串HTML标签* *******/
$str =&#39;<font>高考加油</font><input type="text"/>&#39;;
echo $str . &#39;<hr/>&#39;;
echo htmlspecialchars($str);
function demo($str){
    //定义正则
    $pattern = array(
    &#39;/</S&#39;,
    &#39;/>/S&#39;
    );
    $replace = array(&#39;<&#39;,&#39;>&#39; );
    return preg_replace($pattern, $replace, $str);
}
    echo &#39;<hr/>&#39; ;
    echo demo($str);
?>
Copy after login

代码结果如下所示;

How to use regular replacement to clear all HTML tags in a string?

从上述代码演示结果看出,我们所得到的结果是一样的,这就是我们所实现的替换,然而对于我们今天讨论的清除,道理是一样的,我们还是定义一个正则表达式,

我们以代码为例:

<?php
/**** ***清楚字符串HTML标签* *******/
$str =&#39;<font>高考加油</font><input type="text"/>&#39;;
echo $str . &#39;<hr/>&#39;;
echo htmlspecialchars($str);
function demo($str){
    //定义正则
    $pattern = array(
    &#39;/</S&#39;,
    &#39;/>/S&#39;
    );
    $replace = array(&#39;<&#39;,&#39;>&#39; );
    return preg_replace($pattern, $replace, $str);
}
    echo &#39;<hr/>&#39; ;
    echo demo($str);
    echo &#39;<hr/>&#39;;
    $str =&#39;<font color="" >高考加油</font>&#39;;
    $pattern = &#39;/<.*?>(.*?)<\/.*?>/S&#39;;
    echo preg_replace($pattern,&#39;\1&#39;,$str);
?>
Copy after login

代码结果如下所示;

How to use regular replacement to clear all HTML tags in a string?

通过上述代码我们可以清楚地看到,标签已经清除了,只剩下内容了;

推荐学习:《PHP视频教程

The above is the detailed content of How to use regular replacement to clear all HTML tags in a string?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!