日期:2014-05-17  浏览次数:20440 次

PHP超链接的正则表达式!?
http://detail.tmall.com/item.htm?id=18796056163


http://item.taobao.com/item.htm?spm=a220z.1000881.0.82&id=19150004171
http://item.taobao.com/item.htm?id=16181772778

这样的链接保留id=.... 的部分,其他都替换成
javascrpit:clickItem("18796056163");
javascrpit:clickItem("19150004171");
javascrpit:clickItem("16181772778");



------解决方案--------------------
这么有规律的URL,用explode,就可以了。参考

http://www.php.net/manual/zh/function.explode.php

$part = explode('id=','http://detail.tmall.com/item.htm?id=18796056163');
echo $part[1];

------解决方案--------------------
PHP code

if(preg_match("/id=[\d]+$/",$url,$result)) {
       echo $result[1];
}

------解决方案--------------------
PHP code
$s=<<<txt
<a target="_blank" href="http://detail.tmall.com/item.htm?id=4476817718"></a>
</div>
......
<div class="desc">
<a target="_blank" href="http://detail.tmall.com/item.htm?id=4285064738" class="permalink" style="">描述</a></div>
txt;
echo preg_replace('/href=".+?id=(\d+)"/is',"href=\"javascrpit:clickItem('$1')\"",$s);

------解决方案--------------------
<?php
$s = <<<TEXT
<a target="_blank" href="http://hello.tmall.com/good.html?id=99888889">a</a>
<a target="_blank" href="http://hello.tmall.com/item.html?id=99888889">b</a>
<a target="_blank" href="http://hello.tmall.com/okitd.html?uid=099889999$id=99888889">c</a>
<a target="_blank" href="http://hello.tmall.com/item.html?cid=099889999">d</a>
<a target="_blank" href="http://detail.tmall.com/item.htm?spm=a2106.m874.1000384.d11&id=15393357623&pm2=1&source=dou&scm=1029.0.1.0">e</a>
<a target="_blank" href="http://item.taobao.com/item.htm?id=9999999999999">f</a>
TEXT;

echo preg_replace('/href=".+?item\\.html?.*?[^\\w]id=([0-9]+)(.*)"/i','href="javascrpit:clickItem(\'$1\')"',$s);
?>