日期:2014-05-17 浏览次数:20633 次
<?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 "失败"; }
------解决方案--------------------
方括号中的内容是一个个并列的字符,不是你这样成组使用的
你可能需要的是这样
$pat = '/src="(?!http).*"/iU'; $str = 'src="http://www" src="ftp://www"'; echo preg_replace($pat, 'xxx', $str);
------解决方案--------------------
像这样?非http:,http://www.开头的,补全URL?
$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 />'; }