为什么这个简单的PHP表达式不起作用?

时间:2011-02-02 01:32:29

标签: php

    $links = array('https://google.com', 'http://aloe.com', 'http://foobar.org/image.jpg');

    foreach ($links as $link)
    {
            $unacceptables = array('https:','.doc','.pdf', '.jpg', '.jpeg', '.gif', '.bmp', '.png');
            foreach ($unacceptables as $unacceptable)
            {
                if (strpos($link, $unacceptable) !== false)
                {
                        echo 'not acceptable!<br />';
                }
                else
                {
                        echo 'acceptable<br />';
                }
            }
    }

以上应输出:

not acceptable
acceptable
not acceptable

但相反,如果输出这个烂摊子:

not acceptable!
acceptable
acceptable
acceptable
acceptable
acceptable
acceptable
acceptable
acceptable
acceptable
acceptable
acceptable
acceptable
acceptable
acceptable
acceptable
acceptable
acceptable
acceptable
not acceptable!
acceptable
acceptable
acceptable
acceptable

如何让它正常工作?

4 个答案:

答案 0 :(得分:1)

因为你在循环中得到循环(这就是为什么它输出8 * 3 = 24次)。 你需要引入一个变量$ is_accepted,在内部循环中设置变量,然后在外部循环内输出答案。

  $links = array('https://google.com', 'http://aloe.com', 'http://foobar.org/image.jpg');

foreach ($links as $link)
{
        $unacceptables = array('https:','.doc','.pdf', '.jpg', '.jpeg', '.gif', '.bmp', '.png');
    $is_accepted = true;
        foreach ($unacceptables as $unacceptable)
        {
            if (strpos($link, $unacceptable) !== false)
            {
                    $is_accepted = false;
            }
        }

    if (!$is_accepted)
        {
            echo 'not acceptable!<br />';
        }
        else
        {
            echo 'acceptable<br />';
        }

}

答案 1 :(得分:1)

您希望每个资源只有一个输出,而不是每个资源一个并且不可接受(笛卡尔积!)

试试这个:

$isAcceptable = true;
foreach ( $unacceptables as $unaccetable )
{
    if (strpos($link, $unacceptable) !== false)
    {
        $isAcceptable = false;
        break; // not acceptable, no more checks needed
    }
}
echo ($isAcceptable ? 'acceptable' : 'not acceptable');

而不是你的foreach循环。

答案 2 :(得分:0)

$links = array('https://google.com', 'http://aloe.com', 'http://foobar.org/image.jpg');

foreach ($links as $link)
{
        $unacceptables = array('https:','.doc','.pdf', '.jpg', '.jpeg', '.gif', '.bmp', '.png');
        $accept = true;
        foreach ($unacceptables as $unacceptable)
        {
            if (strpos($link, $unacceptable) !== false)
            {
                    $accept = false;
            }
        }

        if ($accept == true) {
            echo "Acceptable<br />";
        } else {
            echo "Not acceptable<br />";
        }
}

答案 3 :(得分:0)

这是对代码的更正,同时保留您编写的大部分代码:

$links = array('https://google.com', 'http://aloe.com', 'http://foobar.org/image.jpg');

foreach ($links as $link)
{
        $unacceptables = array('https:','.doc','.pdf', '.jpg', '.jpeg', '.gif', '.bmp', '.png');
        $link_is_acceptable = true;
        foreach ($unacceptables as $unacceptable)
        {
            if (strpos($link, $unacceptable) !== false){
                $link_is_acceptable = false;
                break;
            }
        }

        if ($link_is_acceptable)
        {
                echo 'acceptable<br />';
        }
        else
        {
                echo 'not acceptable!<br />';
        }
}
相关问题