我想用`preg_match`判别图片为非广告格式
我所了解的广告照片,`$url`里包含文字:`ad`,`ads`,`admin`,`address`,`reading`,`.gif`。
$match = array('ad', 'ads', 'admin','address','reading','.gif');
if (!preg_match($match, $url)) {
echo $url;//it's not an ad
}
preg_match()是否支持array?如何最精简的正确书写我所需要的结果?
PS:广告URL除了上面我想到的几种外,大家是否还见过其他情况?
谢谢。
我所了解的广告照片,`$url`里包含文字:`ad`,`ads`,`admin`,`address`,`reading`,`.gif`。
$match = array('ad', 'ads', 'admin','address','reading','.gif');
if (!preg_match($match, $url)) {
echo $url;//it's not an ad
}
preg_match()是否支持array?如何最精简的正确书写我所需要的结果?
PS:广告URL除了上面我想到的几种外,大家是否还见过其他情况?
谢谢。
2.你的array里面的每个字串不符合正则规范,可以用implode('|',$match)串成一个正则
3.既然不需要正则,不如直接用字符串函数
foreach($match as $word) {
if(strpos($url, $word) !== flase) {
$check_flag = false;
}
}
if ($check_flag) {
echo $url;//it's not an ad
}
header('Content-type:text/html; charset=utf-8');
require_once 'simple_html_dom.php';
$v = 'http://www.vimeo.com/';
$html = file_get_html($v);
foreach($html->find('img') as $element) {
$url = $element->src;
$match = array('ad', 'ads', 'admin','address','reading','.gif');
foreach($match as $word) {
if(strpos($url, $word) !== flase) {
$check_flag = false;
}
}
if ($check_flag) {
echo '<img src="'.$url.'" /><hr />';
}
}
?>
$match = array('ad', 'ads', 'admin','address','reading','.gif');$check_flag = true;
foreach($match as $word) {
if(strpos($url, $word) !== false) { // false 我写错了
$check_flag = false;
}
}
if ($check_flag) {
echo $url;//it's not an ad
}这样呢
<?php
header('Content-type:text/html; charset=utf-8');
require_once 'simple_html_dom.php';
$v = 'http://www.vimeo.com/';
$html = file_get_html($v);
foreach($html->find('img') as $element) {
$url = $element->src;
echo '<img src="'.$url.'" /><hr />';
}
?>
require_once 'simple_html_dom.php';
$v = 'http://www.vimeo.com/';
$html = file_get_html($v);
$arr = array('ad', 'ads', 'admin','address','reading','.gif');
foreach($html->find('img') as $element) {
$url = $element->src;
$check_flag = false;
foreach($arr as $item) {
if (!strpos($url,$item)) $check_flag = true;
}
if ($check_flag) echo '<img src="'.$url.'" /><hr />';
}
header('Content-type:text/html; charset=utf-8');
require_once 'simple_html_dom.php';
$v = 'http://www.vimeo.com/';
$html = file_get_html($v);
$arr = array('ad', 'ads', 'admin','address','reading','.gif');
foreach($html->find('img') as $element) {
$url = $element->src;
$check_flag = false;
foreach($arr as $item) {
if (!strpos($url,$item)) $check_flag = true;
}
if ($check_flag) echo '<img src="'.$url.'" /><hr />';
reset($arr);
}
下面这个链接带ad,但还是被输出了。
http://ad.doubleclick.net/ad/5480.iac.vimeo/home_logged_out;clipid=;tile=1;sz=300x250;s=vm;ord=19198978?
if (substr_count($url,$item) > 0) $check_flag = true;
break;
} //这样判断呢?
header('Content-type:text/html; charset=utf-8');
require_once 'simple_html_dom.php';
$v = 'http://www.vimeo.com/';
$html = file_get_html($v);
$arr = array('ad', 'ads', 'admin','address','reading','.gif');
foreach($html->find('img') as $element) {
$url = $element->src;
$check_flag = true;
foreach($arr as $item) {
if (!strpos($url,$item)) $check_flag = false;
break;
}
if ($check_flag) echo '<img src="'.$url.'" /><hr />';
reset($arr);
}
//无语了,上面这个还是写错了,下面这个才是必成的。。
header('Content-type:text/html; charset=utf-8');
require_once 'simple_html_dom.php';
$v = 'http://www.vimeo.com/';
$html = file_get_html($v);
$arr = array('ad', 'ads', 'admin','address','reading','.gif');
foreach($html->find('img') as $element) {
$url = $element->src;
$check_flag = true;
foreach($arr as $item) {
if (substr_count($url,$item) > 0) $check_flag = false;
break;
}
if ($check_flag) echo '<img src="'.$url.'" /><hr />';
reset($arr);
}
谢谢大家的帮助。
$v = 'http://www.sina.com.cn/';
可以打印出很多gif的图片……
foreach($arr as $item) {
if (substr_count(strtolower($url),$item) > 0) $check_flag = false;
break;
}