How to use regular expression to delete a tag in php

王林
Release: 2023-02-28 21:02:02
Original
3368 people have browsed it

How to use regular expression to delete a tag in php

目的:

使用正则表达式删除下列内容中的a标签,同时保留a标签内容

<div>欢迎来到php中文网<a href=//www.php.cn>www.php.cn</a></div>
Copy after login

修改后:

<div>欢迎来到php中文网www.php.cn</div>
Copy after login
Copy after login

解决方法:

$str = "<div>欢迎来到php中文网<a href=//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 use regular expression to delete a tag in php. 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