How to check if string contains in array with PHP

2 Answers

0 votes
function contains($s, array $arr) {
    foreach($arr as $w) {
        if (stripos($s, $w) !== false) return true;
    }
    return false;
}

$s1 = "php pro";
$s2 = "php programming";
$arr = array('java', 'c++', 'php programming', 'python');

if (contains($s1, $arr))
    echo "Contains\n";
else
    echo "Not contains\n";

if (contains($s2, $arr))
    echo "Contains\n";
else
    echo "Not contains\n";




/*
run:

Not contains
Contains

*/

 



answered Aug 12, 2020 by avibootz
0 votes
$s1 = "php pro";
$s2 = "php programming";
$arr = array('java', 'c++', 'php programming', 'python');

if (in_array($s1,  $arr))
    echo "Contains\n";
else
    echo "Not contains\n";

if (in_array($s2,  $arr))
    echo "Contains\n";
else
    echo "Not contains\n";




/*
run:

Not contains
Contains

*/

 



answered Aug 12, 2020 by avibootz

Related questions

1 answer 143 views
2 answers 163 views
2 answers 147 views
1 answer 187 views
...