【发布时间】:2010-10-20 06:27:14
【问题描述】:
我有以下几点:
$reg[0] = '`<a(\s[^>]*)href="([^"]*)"([^>]*)>`si';
$reg[1] = '`<a(\s[^>]*)href="([^"]*)"([^>]*)>`si';
$replace[0] = '<a$1href="http://www.yahoo.com"$3>';
$replace[1] = '<a$1href="http://www.live.com"$3>';
$string = 'Test <a href="http://www.google.com">Google!!</a>Test <a href="http://www.google.com">Google!!2</a>Test';
echo preg_replace($reg, $replace, $string);
结果:
Test <a href="http://www.live.com">Google!!</a>Test <a href="http://www.live.com">Google!!2</a>Test
我希望最终得到(不同之处在于第一个链接):
Test <a href="http://www.yahoo.com">Google!!</a>Test <a href="http://www.live.com">Google!!2</a>Test
这个想法是用唯一的其他 URL 替换字符串中链接中的每个 URL。这是一个通讯系统,我想跟踪人们点击了什么,所以 URL 将是一个“假” URL,在记录点击后他们将被重定向到真实 URL。
【问题讨论】:
标签: php regex preg-replace replace