【问题标题】:How to remove certain entries (using regex) from array?如何从数组中删除某些条目(使用正则表达式)?
【发布时间】:2012-07-22 06:17:02
【问题描述】:

我正在编写一个类似于 Facebook 的抓取工具,以从给定的 url 获取信息。我只是完成基本功能的一步。到目前为止的问题是删除无用的图像。 例如,当我运行一些随机网址时,我会得到所有这些图像:

Scraper Object
(
    [url] => http://buzz.money.cnn.com/2012/07/23/spain-italy-short-selling/?iid=HP_Highlight
    [title] => Spain and Italy ban short selling  - The Buzz  - Investment and Stock Market News
    [description] => The Euronext 100 stock index falls sharply on renewed concerns about Spain.    Securities regulators in Spain and Italy both instituted short-selling bans Monday as financial markets tumbled.    The move is designed to limit the downward pressure
    [imageUrls] => Array
        (
            [0] => http://cnnmoneybuzzblog.files.wordpress.com/2012/07/chart_ws_index_euronext100_201272310932-09.png
            [1] => http://i.cdn.turner.com/money/.e1m/img/5.0/data/feargreed/scale.316x95.png
            [2] => http://i2.cdn.turner.com/money/.element/img/5.0/sections/contributors/ben_rooney_130.jpg
            [3] => http://i2.cdn.turner.com/money/.element/img/5.0/sections/contributors/catherine_tymkiw.02.jpg
            [4] => http://i2.cdn.turner.com/money/.element/img/5.0/sections/contributors/paul_lamonica.02.jpg
            [5] => http://i2.cdn.turner.com/money/.element/img/5.0/sections/contributors/hibah_yousuf.02.jpg
            [6] => http://i2.cdn.turner.com/money/.element/img/5.0/sections/contributors/maureen_farrell.02.jpg
            [7] => http://i2.cdn.turner.com/money/.element/img/5.0/sections/contributors/ben_rooney.02.jpg
            [8] => http://i.cdn.turner.com/money/.element/img/4.0/services/button_login.gif
            [9] => http://www.bizographics.com/collect/?fmt=gif&pid=311
            [10] => http://pixel.quantserve.com/pixel/p-5dyPa639IrgIw.gif
            [11] => http://i.cdn.turner.com/money/.element/img/1.0/misc/1.gif
            [12] => http://buzz.money.cnn.com/2012/07/23/spain-italy-short-selling/?iid=HP_Highlight//pixel.quantserve.com/pixel/p-18-mFEk4J448M.gif?labels=%2Clanguage.en%2Ctype.wpcom%2Cposttag.bonds%2Cposttag.dow%2Cposttag.ibex%2Cposttag.italy%2Cposttag.lehman%2Cposttag.milan%2Cposttag.nasdaq%2Cposttag.sp-500%2Cposttag.short-selling%2Cposttag.spain%2Cposttag.stock%2Cposttag.yields%2Cvip.cnnmoneybuzzblog
            [13] => http://stats.wordpress.com/b.gif?v=noscript
        )

)

我只需要找到一种方法来删除所有以 .gif 或 .png 结尾的图像,并将 .jpg 放在数组中,这样用户就可以查看并为文章选择合适的图像。

我尝试了一些数组函数,但我认为这需要一些正则表达式的魔法才能在几乎任何给定的 url 中工作。

附:我可以使用$info->url、$info->description 等访问所有数组数据。只需过滤该数组即可。

【问题讨论】:

  • 离题: CNN Money Buzz 博客出现在我收到的很多垃圾邮件中......
  • 我有一位客户喜欢链接到 Money CNN。我刚刚完成了我的部分编码刮板:P

标签: php arrays filter


【解决方案1】:

不使用正则表达式:

foreach ($objects_array as $obj){
  foreach($obj->imageUrls as $key => $img){
    if(substr($img, -4) === '.gif' || substr($img, -4) === '.png'){
      unset($key);
    }
  }
}

并使用正则表达式:

$pt = '/gif$|png$/';
foreach ($objects_array as $obj){
  foreach($obj->imageUrls as $key => $img){
    $res = preg_match($pt, $img);
    if($res){
      unset($key);
    }
  }
}

【讨论】:

  • 你会恨我我的朋友,但是,它没有显示任何错误,但它仍然显示所有图像文件,包括 gifs 和 pngs。实际上,我删除了第一个 foreach 因为只有一个对象。 $info 仅从一个 url 收集信息。我有 $info->url、$info->title、$info->description 和数组 $info->imageUrls。有什么问题?
  • @Richard-MX 我当然不会恨你 ;) 快速猜测(因为我没有你的代码)尝试将:unset($key); 替换为:unset($info->imageUrls[$key]);
  • 它工作正常!谢谢朋友 :),如果你有一天来坎昆(是的,这里也有程序员 :P)饮料在我身上!
【解决方案2】:

试试array_filter:

http://php.net/manual/en/function.array-filter.php

回调函数应该是一个非常基本的函数,如果您的正则表达式匹配 .png 或 .gif,则返回 false。

【讨论】:

    【解决方案3】:

    您可以在构建数组后进行过滤:

    function only_jpgs($url){
        return substr($url, -4) == '.jpg' ? $url : false;
    }
    
    $info->imageUrls = array_filter($info->imageUrls, "only_jpgs");
    

    但您可能应该在构建数组之前过滤掉图像。

    【讨论】:

    • 我的朋友无法使用这个。当您从 array_filter 调用“only_jpgs”时,函数现在的 $url 是什么?我收到此错误 array_filter() 期望参数 2 是有效的回调,未找到函数“only_jpgs”或无效的函数名 使用它。谢谢。
    • @Richard-MX 如果你不会剪切和粘贴,我帮不了你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    相关资源
    最近更新 更多