首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >php preg_replace

php preg_replace
EN

Stack Overflow用户
提问于 2011-07-21 22:56:36
回答 1查看 282关注 0票数 0

如何使用preg_replace,使用class="country"删除所有链接,并保留链接的文本。

<a href="XXXXXX" class="country">TEXT</a> =>文本

如何修改下面的preg_replace

代码语言:javascript
运行
复制
$str = <<<EOT
Long long ago, there was a poor boy named <a href="index1.html">John</a>. Who was living in <a href="index2.html">Liverpoor</a>, a city of <a href="index3.html" class="country">england</a>.
EOT;
$result= preg_replace('/<a(.*?)class="country"(.*?)>(.*?)<\/a>/i','$3',$str);
echo $result;
// I want get the result as: "Long long ago, there was a poor boy named <a href="index1.html">John</a>. Who was living in <a href="index2.html">Liverpoor</a>, a city of england"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-21 23:28:24

这将适用于大多数情况:

代码语言:javascript
运行
复制
$result= preg_replace('/<a [^>]*class="country"[^>]*>([^<]+)<\/a>/i','$1',$str);

但请考虑使用DOM解析器

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6778120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档