How to clear a tag in php

藏色散人
Release: 2023-03-10 07:56:02
Original
2267 people have browsed it

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>
Copy after login

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

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

解决方法:

$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;
Copy after login

运行结果为:

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

推荐学习:《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!

Related labels:
php
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