php preg_replace基于索引匹配

时间:2015-10-27 09:35:16

标签: preg-replace

我有一个包含2个标签的字符串。我想将其中一个替换为另一个字符串。我的代码如下:

$pattern = "/<a\s+href=\"(http|https)\:\/\/[^\"]+\">/";
$content = "I have a link <a href=\"http://www.yahoo.com/\">link text </a> and I have another link <a href=\"http://www.google.com/\">Google</a>";
$content = preg_replace($pattern, "<a title='title' href=' '>", $content);
    echo $content;

上面的代码将为两个标签添加title属性。但是,我想只添加title属性并在标记值为&#34; google时删除href值。&#34;。有没有办法做到这一点?

0 个答案:

没有答案
相关问题