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

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

WBOY
Release: 2016-06-13 13:16:27
Original
756 people have browsed it

求高手,看下这段正则哪里错了??
需求:将字符串$str 内的 href内的超链接提取出来,如果提取出来的超链接包含"id=数字"的话,将其替换为go.php?id=数字,否则的话替换为#,并将替换后的链接放回原处,现在的情况是,虽然可以提取,但是在放回原处时把整个标签全部都给替换了,我现在只想替换href内的网址,哪里出错了??

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


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

echo $str;


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

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

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

Copy after login


------解决方案--------------------
$str = preg_replace('#(?

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template