如何用同一内容中的所有链接之一替换内容?

时间:2019-07-07 09:08:05

标签: regex preg-replace

我正在尝试使用正则表达式(preg_replace)将我的wordpress发布内容中的所有文本,链接和其他html属性替换为同一内容中的特定链接。这样唯一的输出就变成<a class="lego" href="https://www.example.com/sample-post/">The anchor</a>

例如:

<?php $content = 'Hello, it feels good to be <b>around</b> today. Currently am working on a wordpress site <a class="lego" href="https://www.example.com/sample-post/">The anchor</a>, so feel free to check it out at <a href="https://www.example.com/sample-post/">The anchor</a> since we've got you covered'

echo preg_replace('.*? <a class="lego" href="(.+)">(.+)</a>.*?', '<a class="lego" href="$1">$2</a>'), $content);?>

试图提取并用于替换内容的链接是具有class="lego"

的链接

问题在于替换整个内容。因此,即使我点击了源代码,输出也不会显示链接,什么也不输出。

0 个答案:

没有答案