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; echo preg_replace('/href=".+?id=(\d+)"/is',"href=\"javascrpit:clickItem('$1')\"",$s);
------解决方案--------------------
<?php
$s = a
b
c
d
e
f
TEXT;
echo preg_replace('/href=".+?item\\.html?.*?[^\\w]id=([0-9]+)(.*)"/i','href="javascrpit:clickItem(\'$1\')"',$s);
?>