PHP - check if a string contains any character in another string

How to check if a string contains any character in another string with PHP?

$a = "asd";
$b = "ds";
if (if_first_string_contains_any_of_the_chars_in_second_string($a, $b)) {
    echo "Yep!";
}

So, in this case, it should echo since the ASD contains both D and S.

I want to do this without regular expressions .

+4
source share
4 answers

You can do this using

$a = "asd";
$b = "ds";
if (strpbrk($a, $b) !== FALSE)
    echo 'Found it';
else
    echo "nope!";

More details: http://php.net/manual/en/function.strpbrk.php

The second parameter case sensitive.

+8
source

try using strpos, hope it will be useful

+1
source

+1 @hardik solanki. , Similar_text (count/percent ).

$a = "asd";
$b = "ds";
if (similar_text($a, $b)) {
  echo "Yep!";
}

: .

+1

you can use the str_split function here, and then just look at the links provided: this may help you,

http://www.w3schools.com/php/showphp.asp?filename=demo_func_string_str_split http://www.w3schools.com/php/func_array_intersect.asp

0
source

All Articles