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).'
';}