Home >Backend Development >PHP Tutorial >帮忙写一个正则,替换超连接。谢谢!

帮忙写一个正则,替换超连接。谢谢!

WBOY
WBOYOriginal
2016-06-23 14:19:50701browse

我想替换超连接中的 http://www.baidu.com 变为 http://google.com/?url=http://www.baidu.com

现在找到一代码可以替换,但是不知道先怎么取出 http://www.baidu.com 然后加上 http://google.com/?url= 然后替换回去,变为 http://google.com/?url=http://www.baidu.com
的效果。

$str = '点击观看棋谱';    echo preg_replace("/(?]*)(?=\>)/i","#", $str); 



能帮写一个替换正则吗! 
替换字符串中所有的超级连接

谢谢了啊


回复讨论(解决方案)

这样应该OK

echo preg_replace("/(?]*?)(?=\")/i","http://google.com/?url=\$1", $str);

 $str = '<a target="_blank" href="http://www.baidu.com" class="akey">点击观看棋谱</a><a target="_blank" href="http://www.baidu.com" class="akey">点击观看棋谱</a><a target="_blank" href="http://www.baidu.com" class="akey">点击观看棋谱</a>';      echo preg_replace("/(?<=href=\")([^\>\"]*)(?=\")/i","http://google.com/?url=$0", $str);
 

非常感谢!!  可以用 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn