Rumah >pembangunan bahagian belakang >tutorial php >这条简单的正则如何写。
这条简单的正则怎么写。。
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php $pat = "!src=\"([^(\"|http)]*)\"!ieU";$str = 'src="http://www" ';if(preg_match($pat,$str)){ echo "匹配成功";}else{ echo "失败";}
$pat='/(src\=[\"\']?http)+|(href\=[\"\']?http)+/';$str = 'src="http://www" ';if(preg_match($pat,$str)){ echo "匹配成功";}else{ echo "失败";}<br><font color="#e78608">------解决方案--------------------</font><br>方括号中的内容是一个个并列的字符,不是你这样成组使用的<br><br>你可能需要的是这样<br>
$pat = '/src="(?!http).*"/iU';$str = 'src="http://www" src="ftp://www"';echo preg_replace($pat, 'xxx', $str);<br><font color="#e78608">------解决方案--------------------</font><br>像这样?非http:,http://www.开头的,补全URL?<br><br>
$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).'<br>';}<div class="clear"> </div>