Home >Backend Development >PHP Tutorial >求高手,看下这段正则错哪了?

求高手,看下这段正则错哪了?

WBOY
WBOYOriginal
2016-06-13 11:08:55806browse

求高手,看下这段正则哪里错了??

本帖最后由 dz215136304 于 2012-07-05 08:54:58 编辑 需求:将字符串$str 内的 href内的超链接提取出来,如果提取出来的超链接包含"id=数字"的话,将其替换为go.php?id=数字,否则的话替换为#,并将替换后的链接放回原处,现在的情况是,虽然可以提取,但是在放回原处时把整个标签全部都给替换了,我现在只想替换href内的网址,哪里出错了??

$str='
求高手,看下这段正则错哪了?';


$str=preg_replace('[a-zA-z]+://[^\s]*','endecode::showdescurl("$2")',$str);

echo $str;


endecode类的静态方法:showdescurl代码如下:

<br />static function showdescurl($url){<br />		 if(strpos($url,'id') !==false){<br />        preg_match('/id=(\d+)/i',$url,$ar);<br />        return  'go.php?id='.$ar[1];<br />     }else{<br />        return '#';     <br />     }  <br />		<br />}<br />

------解决方案--------------------
$str = preg_replace('#(?
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