PHP - 检查一个字符串是否包含另一个字符串中的任何字符


PHP - Check if a string contains any of the chars in another string

如何使用

PHP 检查一个字符串是否包含另一个字符串中的任何字符?

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

所以在这种情况下,它应该回显,因为ASD同时包含D和S。

我想在没有正则表达式的情况下做到这一点。

你可以

使用

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

欲了解更多信息,请查看 : http://php.net/manual/en/function.strpbrk.php

第二个参数是 case sensitive

+1 @hardik solanki。此外,您可以使用similar_text(两个字符串中匹配字符的计数/百分比(。

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

注意:函数区分大小写。

你可以在这里使用str_split函数,然后看看给定的链接:这可能会对你有所帮助,

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

尝试使用 strpos.希望对您有所帮助