phpstrpos

stripos() == false gives unintended result


What is wrong with this code? I've tried using array_udiff without any success.

<?php
#I want to echo values of $paths1 that do not appear (even partially) on $paths2.

$paths1 = array('one', 'two', 'three');
$paths2 = array('twenty one', 'twenty two');

foreach ($paths1 as $path1)
{
    foreach ($paths2 as $path2)
    {
        if (stripos($path1, $path2) == False)
        {
            echo $path1 . "<br>";
            break;
        }
    }
    echo "<br>";
}
?>

Solution

    1. You need to use stripos() === false, as if they match it's going to return 0 which is == to false.

    2. You have your parameters mixed, it should be stripos($path2, $path1).

    3. You need to check all values in $paths2 until you find one it is in. You are saying it's not in any $paths2 after the first one you don't find it in. Set a flag of $flag = true; between the foreach() loops. Instead of echoing inside the second foreach, just set $flag == false if stripos($path2, $path1) !== false. After the second loop ends, but before the first, output if $flag == false.

    ie

    foreach ($paths1 as $path1)
    {
        $flag = true;
        foreach ($paths2 as $path2)
        {
             if (stripos($path2, $path1) !== false)
             {
                 $flag = false;
                 break;
             }
        }
        if($flag)
            echo $path1;
    }
    

    Note: didn't test it, but should work.