Home  >  Article  >  Backend Development  >  PHP超链接的正则表达式!该如何解决

PHP超链接的正则表达式!该如何解决

WBOY
WBOYOriginal
2016-06-13 10:46:35888browse

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];}<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code
$s=......<div class="desc"><a target="_blank" href="http://detail.tmall.com/item.htm?id=4285064738" class="permalink" >描述</a></div>txt;echo preg_replace('/href=".+?id=(\d+)"/is',"href=\"javascrpit:clickItem('$1')\"",$s);<br><font color="#e78608">------解决方案--------------------</font><br><?php <br />$s = <a target="_blank" href="http://hello.tmall.com/good.html?id=99888889">a</a><br><a target="_blank" href="http://hello.tmall.com/item.html?id=99888889">b</a><br><a target="_blank" href="http://hello.tmall.com/okitd.html?uid=099889999%24id=99888889">c</a><br><a target="_blank" href="http://hello.tmall.com/item.html?cid=099889999">d</a><br><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><br><a target="_blank" href="http://item.taobao.com/item.htm?id=9999999999999">f</a><br>TEXT;<br><br>echo preg_replace('/href=".+?item\\.html?.*?[^\\w]id=([0-9]+)(.*)"/i','href="javascrpit:clickItem(\'$1\')"',$s);<br>?><div class="clear">
                 
              
              
        
            </div>
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