PHP preg_replace:如果超链接包含';youtube.com';


PHP preg_replace: Need to modify hyperlink text if hyperlink contains 'youtube.com'

我快到了!

这是我试图调整的字符串,以及我的preg_replace尝试。

$description_string = '<p>Here is the test link: <a href="http://www.youtube.com/watch?v=2MFn8L9tIrg" target="_blank">“Man or Muppet”</a> with other text afterwards.</p>';

$description = preg_replace( '/(<a[^>]+youtube[^>]*>)+[^"]*(<'/a>)+/', '$0Watch This Video$2', $description );

我得到的结果不正确:

这是测试链接:"男人还是布偶"观看这段视频和其他文字。

如有任何帮助,我们将不胜感激!谢谢

好吧,不确定你是否试图在那里获得实际的标题。但我想到的是:

<?php
$description_string = '<p>Here is the test link: <a href="http://www.youtube.com/watch?v=2MFn8L9tIrg" target="_blank">''Man or Muppet''</a> with other text afterwards.</p>';
$description = preg_replace( '/(<a[^>]+youtube[^>]*>)+[^"]*(<'/a>)+/', '$1Watch This Video$2', $description_string );
echo $description;
?>

结果:

<p>Here is the test link: <a href="http://www.youtube.com/watch?v=2MFn8L9tIrg" target="_blank">Watch This Video</a> with other text afterwards.</p>

你最大的问题是标题中的引号(")。它切断了锚标记。使用$0也是不正确的。你需要使用$1。

这可能不是你所需要的,但它对你来说是一个快速的猴子补丁。