php 预替换
如何使用 preg_replace,删除所有带有 class="country"
的链接并保留链接的文本。
如何修改下面的preg_replace
?
$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"
How to use preg_replace, remove all the links with class="country"
and remain the links' text.
<a href="XXXXXX" class="country">TEXT</a>
=> TEXT
How to modify the preg_replace
below?
$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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这适用于大多数情况:
但请考虑使用 DOM 解析器
This will work with most cases:
but please consider using DOM parser instead