开发者

Why is this code using stripos() not working?

stripos() doesn't seem to be working like it should, here's my code:

$regex_blitz = array("#bols", "#Blitz", "#Blitz Ipva", "#detran", "#ipva", "biltz");
foreach($regex_blitz as $blitz)
{
    echo $blitz;
    echo "</br>";
    echo $string;
    echo "</br>";
    if(stripos($string, $blitz))
    {
        echo 'aqui';
        }
                else
        {
                echo 'oi';echo "</br>";
    }
}

here is the output:

#bols
#Blitz av das americas sentido recreio, pista lateral. Em frente ao Ribalta!
oi
#Blitz
#Blitz av das americas sentido recreio, pista lateral. Em frente ao Ribalta!
oi
#Blitz Ipva
#Blitz av das americas sentido recreio, pista lateral. Em frente ao Ribalta!
oi
#detran
#Blitz av das americas sentido recreio, pista lateral. Em frente ao Ribalta!
oi
#ipva
#Blitz av das americas sentido recreio, pista lateral. Em frente ao Ribalta!
oi
biltz
#Blitz av das americas sentido recreio, pista lateral. Em frente ao Ribalta!
oi

You can notice that when $blitz is '#Blitz' it was supposed to pass the 'if', help me !

ju开发者_Python百科st noticed that if I move "#blitz" forward in the string it works, but I can't do that since it searches automatically, is this a bug ?


The problem is that stripos returns 0, if the match is at the beginning of the string. You need to check it using !== false. http://php.net/manual/en/function.stripos.php

$regex_blitz = array("#bols", "#Blitz", "#Blitz Ipva", "#detran", "#ipva", "biltz");
foreach($regex_blitz as $blitz)
{
    echo $blitz;
    echo "</br>";
    echo $string;
    echo "</br>";
    if(stripos($string, $blitz) !== false)
    {
        echo 'aqui';
        }
                else
        {
                echo 'oi';echo "</br>";
    }
}


Someone oversaw big warning sign:

    Warning
This function may return Boolean FALSE, but may also return a non-Boolean value
which evaluates to FALSE, such as 0 or "".

http://php.net/stripos

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜