Home  >  Article  >  Backend Development  >  How to clear a tag in php

How to clear a tag in php

藏色散人
藏色散人Original
2021-06-01 09:06:292257browse

php清除a标签的方法:首先创建一个PHP示例文件;然后通过“htmlspecialchars_decode”及“preg_replace”方法清除a标签并保留内容即可。

How to clear a tag in php

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

php怎么清除a标签?

本文实例讲述了PHP正则删除html代码中a标签并保留标签内容的方法。

问题:

有HTML代码如:

<div>欢迎访问PHP中文网<a href=https://www.php.cn>www.php.cn</a></div>

要求正则删除a标签,同时保留a标签内容,如下:

<div>欢迎访问PHP中文网www.php.cn</div>

解决方法:

$str = "<div>欢迎访问PHP中文网<a href=https://www.php.cn>www.php.cn</a></div>";
$str = htmlspecialchars_decode($str);
$str = preg_replace("/]*>(.*?)<\/a>/is", "$1", $str);
echo $str;

运行结果为:

<div>欢迎访问PHP中文网www.php.cn</div>

推荐学习:《PHP视频教程

The above is the detailed content of How to clear a tag in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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