How to write this simple regular expression.

Source: Internet
Author: User
How to write this simple regular expression .. PHPcode & lt ;? Php $ pat & quot ;! Src & quot; ([^ (& quot; | http)] *) & quot ;! IeU & quot; $ strsrc & quot; www & quot; if (preg_match ($ pat, $ str) {echo & quot; matches this simple regular expression ..
PHP code
  
  


I want to match href = src =. if the content is not replaced with the content starting with http, it will not be replaced with the content starting with http.
The above regular expression is wrong ....

------ Solution --------------------
I have not found any problems.
PHP code
$ Pat = '/(src \ = [\ "\']? Http) + | (href \ = [\ "\ ']? Http) +/'; $ str = 'src = "http: // www"'; if (preg_match ($ pat, $ str) {echo "matched ";} else {echo "failed ";}
------ Solution --------------------
The content in square brackets is a parallel character, not used in groups like this.

What you might need is
PHP code
$ Pat = '/src = "(?! Http ). * "/iU '; $ str = 'src =" http: // www "src =" ftp: // www "'; echo preg_replace ($ pat, 'XXX ', $ str );
------ Solution --------------------
Like this? If it is not prefixed with http:, http: // www., complete the URL?

PHP code
$testurls = array(    'http://www.freenewspos.com/italia/creativecommons-photo-album#!POS__calcio&num=0',    'freenewspos.com/italia/creativecommons-photo-album#!POS__NBA&num=0',    'www.freenewspos.com/italia/creativecommons-photo-album#!POS__Twilight&num=0'); foreach($testurls as $singleurl){    echo preg_replace('#(?:http(s)?://)?(?:www\.)?(.+)#', 'http\1://www.\2', $singleurl).'
';}

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.