我想用`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除了上面我想到的几种外,大家是否还见过其他情况?
谢谢。

解决方案 »

  1.   

    1.不能
    2.你的array里面的每个字串不符合正则规范,可以用implode('|',$match)串成一个正则
    3.既然不需要正则,不如直接用字符串函数
      

  2.   

    如果仅仅是用多个关键字的话,用正则不如用定位正则支持 或者 (aa|bb)$match = array('ad', 'ads', 'admin','address','reading','.gif');$check_flag = true;
    foreach($match as $word) {
        if(strpos($url, $word) !== flase) {
            $check_flag = false;
        }
    }
    if ($check_flag) {
        echo $url;//it's not an ad
    }
      

  3.   

    hnxxwyq 定位看上去很简洁,但是现在一张照片都没有了……<?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;
    $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 />';
    }        
    }
    ?>
      

  4.   


    $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
    }这样呢
      

  5.   

    还是输出一片空白,不加你的判断,可以正常输出照片的,只不过有一些是广告图片。
    <?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 />';
        }
    ?>
      

  6.   

    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 />';
    }
      

  7.   

    //忘了复位,用下面再试试:
    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);
    }
      

  8.   

    T5500,图有了,广告还是没有被去掉……
    下面这个链接带ad,但还是被输出了。
    http://ad.doubleclick.net/ad/5480.iac.vimeo/home_logged_out;clipid=;tile=1;sz=300x250;s=vm;ord=19198978?
      

  9.   

        foreach($arr as $item) {
            if (substr_count($url,$item) > 0) $check_flag = true;
            break;
        }  //这样判断呢?
      

  10.   

    //好了,下面这个必成了
    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);
    }
      

  11.   


    //无语了,上面这个还是写错了,下面这个才是必成的。。
    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);
    }
      

  12.   

    great~,应该没有问题了。我再找几个网站测试一下,如果没问题,晚点就结分给大家。
    谢谢大家的帮助。
      

  13.   

    T5500, 10楼那个,惊叹号加在 if (!$check_flag),貌似也是必成的 :}
      

  14.   

    @T5500 还有问题, gif还是没有被去掉。换成新浪的网址,
    $v = 'http://www.sina.com.cn/';
    可以打印出很多gif的图片……
      

  15.   

    是不是大小写的原因,我试了一下,如果是.gif的话,能够过滤的,.GIF就不行。
      

  16.   

    //判断语修改一下再试试
        foreach($arr as $item) {
            if (substr_count(strtolower($url),$item) > 0) $check_flag = false;
            break;
        }