当前位置:  开发笔记 > 编程语言 > 正文

PHP Array过滤正则表达式

如何解决《PHPArray过滤正则表达式》经验,为你挑选了1个好方法。

大家好我有一个如下所示的阵列

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
)

基本上,我想

    删除每个空数组元素

    删除".jpg,.png,.gif"名称中没有扩展名的每个数组项;

    最后删除包含关键字的数组项"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 ) 

我只想要网址.我认为我有从原始内容中提取网址的问题.lemme发布了一个关于原始问题和我正在做什么的链接.

RSS Feed和图像提取深入

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


好的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;         
    }

示例用法.$这 - > 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 "

"; }

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

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

我们差点在那里的人...任何想法



1> GZipp..:

使用例如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);

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

推荐阅读
郑谊099_448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有