Please take a look at what is wrong with this regular expression? -Php Tutorial

Source: Internet
Author: User
Please take a look at what is wrong with this regular expression ?? This post is finally edited by dz215136304 from 2012-07-0508: 54: 58. the hyperlink in & nbsp; href in the string $ str & nbsp; is extracted, replace the extracted hyperlink with go. php? Id number. otherwise, replace it with # and put the replaced link back to its original location. the current situation is to be a master. what is wrong with this regular expression ??

This post was last edited by dz215136304 at 08:54:58

Requirement: extract the hyperlink from the href in the string $ str. if the extracted hyperlink contains "id = number", replace it with go. php? Id = number. otherwise, replace it with # and put the replaced link back to its original location. the current situation is that although it can be extracted, but I replaced all the labels when I put them back to the original position. now I only want to replace the URLs in href. what is the error ??

$ Str = '';


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

Echo $ str;


The static method of the endecode class: showdescurl code is as follows:


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

}

------ Solution --------------------
$ Str = preg_replace ('#(? <= Href ...) ([a-zA-z] +: //) ([^ \ '"] *) # e', 'endecode: showdescurl (" $2 ")', $ str );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.