【问题标题】:PHP Array filter regular expressionsPHP 数组过滤正则表达式
【发布时间】:2011-04-17 05:56:15
【问题描述】:

大家好,我有一个如下所示的数组

Array
(
    [0] => http://api.tweetmeme.com/imagebutton.gif?url=http://mashable.com/2010/09/25/trailmeme/ 
    [1] => http://cdn.mashable.com/wp-content/plugins/wp-digg-this/i/gbuzz-feed.png 
    [2] => http://mashable.com/wp-content/plugins/wp-digg-this/i/fb.jpg 
    [3] => http://mashable.com/wp-content/plugins/wp-digg-this/i/diggme.png 
    [4] => http://ec.mashable.com/wp-content/uploads/2009/01/bizspark2.gif 
    [5] => http://cdn.mashable.com/wp-content/uploads/2010/09/web.png 
    [6] => http://mashable.com/wp-content/uploads/2010/09/Screen-shot-2010-09-24-at-10.51.26-PM.png 
    [7] => http://cdn.mashable.com/wp-content/uploads/2009/02/bizspark.jpg 
    [8] => http://feedads.g.doubleclick.net/~at/lxx00QTjYBaYojpnpnTa6MXUmh4/0/di 
    [9] => 
    [10] => http://feedads.g.doubleclick.net/~at/lxx00QTjYBaYojpnpnTa6MXUmh4/1/di 
    [11] => 
    [12] => http://feeds.feedburner.com/~ff/Mashable?i=0N_mvMwPHYk:j5Pmi_N-JQ8:D7DqB2pKExk 
    [13] => 
    [14] => http://feeds.feedburner.com/~ff/Mashable?i=0N_mvMwPHYk:j5Pmi_N-JQ8:V_sGLiPBpWU 
    [15] => 
    [16] => http://feeds.feedburner.com/~ff/Mashable?i=0N_mvMwPHYk:j5Pmi_N-JQ8:F7zBnMyn0Lo 
    [17] => 
    [18] => http://feeds.feedburner.com/~ff/Mashable?d=qj6IDK7rITs 
    [19] => 
    [20] => http://feeds.feedburner.com/~ff/Mashable?d=_e0tkf89iUM 
    [21] => 
    [22] => http://feeds.feedburner.com/~ff/Mashable?i=0N_mvMwPHYk:j5Pmi_N-JQ8:gIN9vFwOqvQ 
    [23] => 
    [24] => http://feeds.feedburner.com/~ff/Mashable?d=yIl2AUoC8zA 
    [25] => 
    [26] => http://feeds.feedburner.com/~ff/Mashable?d=P0ZAIrC63Ok 
    [27] => 
    [28] => http://feeds.feedburner.com/~ff/Mashable?d=I9og5sOYxJI 
    [29] => 
    [30] => http://feeds.feedburner.com/~ff/Mashable?d=CC-BsrAYo0A 
    [31] => 
    [32] => http://feeds.feedburner.com/~ff/Mashable?i=0N_mvMwPHYk:j5Pmi_N-JQ8:_cyp7NeR2Rw 
    [33] => 
    [34] => http://feeds.feedburner.com/~r/Mashable/~4/0N_mvMwPHYk
)

基本上,我想

  1. 删除每个空数组元素
  2. 删除每个数组项 在其名称中扩展 ".jpg,.png,.gif"
  3. 最后删除包含"digg,fb,tweet,bizspark"等关键字的数组项。

已经尝试过你的代码,它返回例如 嗨,我试过上面的代码......它返回一个包含我想要的东西的数组。

嗨,我试过上面的代码......它返回一个包含我想要的东西的数组。 )

Array ( [5] =>
http://feedads.g.doubleclick.net/~at/W-z_kHMi30EtE1mpxK8NvMmNmeg/0/di
[7] =>
http://feedads.g.doubleclick.net/~at/W-z_kHMi30EtE1mpxK8NvMmNmeg/1/di
[9] =>
http://feeds.feedburner.com/~ff/Mashable?i=mEedXAp78pg:339cIishd6A:D7DqB2pKExk
[11] =>
http://feeds.feedburner.com/~ff/Mashable?i=mEedXAp78pg:339cIishd6A:V_sGLiPBpWU
[13] =>
http://feeds.feedburner.com/~ff/Mashable?i=mEedXAp78pg:339cIishd6A:F7zBnMyn0Lo
[15] =>
http://feeds.feedburner.com/~ff/Mashable?d=qj6IDK7rITs
[17] =>
http://feeds.feedburner.com/~ff/Mashable?d=_e0tkf89iUM
[19] =>
http://feeds.feedburner.com/~ff/Mashable?i=mEedXAp78pg:339cIishd6A:gIN9vFwOqvQ
[21] =>
http://feeds.feedburner.com/~ff/Mashable?d=yIl2AUoC8zA
[23] =>
http://feeds.feedburner.com/~ff/Mashable?d=P0ZAIrC63Ok
[25] =>
http://feeds.feedburner.com/~ff/Mashable?d=I9og5sOYxJI
[27] =>
http://feeds.feedburner.com/~ff/Mashable?d=CC-BsrAYo0A
[29] =>
http://feeds.feedburner.com/~ff/Mashable?i=mEedXAp78pg:339cIishd6A:_cyp7NeR2Rw
[31] =>
http://feeds.feedburner.com/~r/Mashable/~4/mEedXAp78pg
))

)

我希望它从第一个示例返回例如

[5] => http://cdn.mashable.com/wp-content/uploads/2010/09/web.png 
    [6] => http://mashable.com/wp-content/uploads/2010/09/Screen-shot-2010-09-24-at-10.51.26-PM.png 

有什么想法吗?


嗨,GZIp,我已经修改了代码,我得到了更好的结果

function url_array_filter($url)
{
    static $words = array('digg', 'fb', 'tweet', 'bizspark','feedburner','feedads','CountImage');
    static $extens = array('.jpg', '.png', '.gif');
    $ret = true;
    if (!$url) {
        $ret = false;
    } elseif (str_replace($words, '', $url) != $url) {
        $ret = false;
    } else {
        $path = parse_url($url, PHP_URL_PATH);
        if (in_array(substr($path, -4), $extens)) {
            $ret = false;
        }
    }
    return $ret;
} 

我的问题现在与输出有关。例如

Array ( [0] => http://cdn.dzone.com/images/thumbs/120x90/491551.jpg' style='width:120;height:90;float:left;vertical-align:top;border:1px solid ) 

Array ( [0] => http://cdn.dzone.com/images/thumbs/120x90/490913.jpg' style='width:120;height:90;float:left;vertical-align:top;border:1px solid ) 

我只想要网址。我想我在从原始内容中提取 url 时遇到了问题。让我发布原始问题的链接以及我在做什么。

RSS Feeds and image extraction indepth

我只想要网址。我认为从那个链接.... getImagesUrl() 可能搞砸了。我将尝试使用 parse_url 来恢复正确的 url。让我知道我是否在正确的轨道上。我非常接近管理从用 magpie 解析的 rss 提要中提取图像 url


好的 GZip,这是我添加到您的代码中的修改和添加... 95% 有效!伟大的。 虽然我确实收到了一些有趣的结果,我在下面发布了

function url_array_filter($url)
{
    static $words = array('digg', 'fb', 'tweet', 'bizspark','feedburner','feedads','CountImage','fuelbrand');
    static $extens = array('.jpg', '.png', '.gif');
    $ret = true;
    if (!$url) {
        $ret = false;
    } elseif (str_replace($words, '', $url) != $url) {
        $ret = false;
    } else {
        $path = parse_url($url, PHP_URL_PATH);
        if (in_array(substr($path, -4), $extens)) {
            $ret = false;
        }
    }
    return $ret;
} 

function cleanURL($a_url)
    {
    $ret=array();
    foreach ($a_url as $c)
        {
        $a=parse_url($c, PHP_URL_SCHEME).'://'.parse_url($c, PHP_URL_HOST).parse_url($c, PHP_URL_PATH);    
        $a=explode("'",$a);
        $ret[]=$a[0];
        }
    return $ret;         
    }

示例用法。 $this->getImagesUrl($c);下面返回第一个问题的结果。

                    foreach($content as $c) {
                        // get the images in content
                        $arr = $this->getImagesUrl($c);
                        $arr = array_filter($arr, 'url_array_filter');
                        }
                    $ret=cleanURL($arr);
                    if (count($ret)>0)
                        {
                        print_r($ret);                                
                        echo "<br/><br/>";
                        }

到目前为止,几乎一切都很好,但我不断得到一些不好的结果,比如

Array ( [0] => http://cdn.mashable.com/wp-content/uploads/2010/02/ipad-side- )
Array ( [0] => http://mrg.bz/FZtr2k [1] => http://mrg.bz/IDkx4w ) 

我们几乎在那里的人......任何想法

【问题讨论】:

  • 到目前为止你的代码是什么样子的?
  • 我不知道如何使用正则表达式......所以不能再进一步了。我现在所能做的就是删除空数组元素
  • 你不需要正则表达式。使用stristr 和比较功能发挥创意。

标签: php regex arrays filter


【解决方案1】:

使用例如array_filter() 将为您提供灵活性和易于维护(更改需求、调试等):

function url_array_filter($url)
{
    static $words = array('digg', 'fb', 'tweet', 'bizspark');
    static $extens = array('.jpg', '.png', '.gif');
    $ret = true;
    if (!$url) {
        $ret = false;
    } elseif (str_replace($words, '', $url) != $url) {
        $ret = false;
    } else {
        $path = parse_url($url, PHP_URL_PATH);
        if (in_array(substr($path, -4), $extens)) {
            $ret = false;
        }
    }
    return $ret;
}

$arr = array_filter($arr, 'url_array_filter');
print_r($arr);

(适用于给定的数组,但可能需要更改;这是演示代码。)

【讨论】:

  • 将 substr($path, -4) 更改为 strrchr($path, '.') 将摆脱整数常量。
【解决方案2】:
foreach ($array as $key => $value) {
    if (
        empty($value)||
        (preg_match('#^http:\/\/(.*)\.(gif|png|jpg)$#i', $value) == 0)||
        (preg_match('#(tweet|bizspark)#i', $value) > 0)
    ) {
        unset($array[$key]);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    相关资源
    最近更新 更多