将字符串与 PHP 中的字符串数组进行比较?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8891937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:43:22  来源:igfitidea点击:

Compare string against array of strings in PHP?

phparraysstring

提问by EOB

I have a string like abcdefg123hijklm. I also have an array which contains several strings. Now I want to check my abcdefg123hijklmand see if the 123from abcdefg123hijklmis in the array. How can I do that? I guess in_array()wont work?

我有一个像abcdefg123hijklm. 我还有一个包含多个字符串的数组。现在我想检查我的abcdefg123hijklm,看看123fromabcdefg123hijklm是否在数组中。我怎样才能做到这一点?我想in_array()行不通?

Thanks?

谢谢?

回答by bardiir

So you want to check if any substring of that particular string (lets call it $searchstring) is in the array? If so you will need to iterate over the array and check for the substring:

所以你想检查该特定字符串的任何子字符串(让我们称之为$searchstring)是否在数组中?如果是这样,您将需要遍历数组并检查子字符串:

foreach($array as $string)
{
  if(strpos($searchstring, $string) !== false) 
  {
    echo 'yes its in here';
    break;
  }
}

See: http://php.net/manual/en/function.strpos.php

请参阅:http: //php.net/manual/en/function.strpos.php

If you want to check if a particular part of the String is in the array you will need to use substr()to separate that part of the string and then use in_array()to find it.

如果您想检查字符串的特定部分是否在数组中,您将需要使用substr()来分隔字符串的该部分,然后使用in_array()它来查找它。

http://php.net/manual/en/function.substr.php

http://php.net/manual/en/function.substr.php

回答by Nathan Stretch

Another option would be to use regular expressions and implode, like so:

另一种选择是使用正则表达式并内爆,如下所示:

if (preg_match('/'.implode('|', $array).'/', $searchstring, $matches))
    echo("Yes, the string '{$matches[0]}' was found in the search string.");
else
    echo("None of the strings in the array were found in the search string.");

It's a bit less code, and I would expect it to be more efficient for large search strings or arrays, since the search string will only have to be parsed once, rather than once for every element of the array. (Although you do add the overhead of the implode.)

它的代码少了一点,我希望它对于大型搜索字符串或数组更有效,因为搜索字符串只需要解析一次,而不是对数组的每个元素解析一次。(尽管您确实增加了内爆的开销。)

The one downside is that it doesn't return the array index of the matching string, so the loop might be a better option if you need that. However, you could also find it with the code above followed by

一个缺点是它不返回匹配字符串的数组索引,因此如果需要,循环可能是更好的选择。但是,您也可以使用上面的代码找到它,后跟

$match_index = array_search($matches[0], $array);

Edit: Note that this assumes you know your strings aren't going to contain regular expression special characters. For purely alphanumeric strings like your examples that will be true, but if you're going to have more complex strings you would have to escape them first. In that case the other solution using a loop would probably be simpler.

编辑:请注意,这假设您知道您的字符串不会包含正则表达式特殊字符。对于像您的示例这样的纯字母数字字符串,这将是正确的,但是如果您要拥有更复杂的字符串,则必须先对它们进行转义。在这种情况下,使用循环的其他解决方案可能会更简单。

回答by ABorty

You can do it reversely. Assume your string is $string and array is $array.

你可以反过来做。假设您的字符串是 $string,数组是 $array。

foreach ($array as $value)
{
    // strpos can return 0 as a first matched position, 0 == false but !== false
    if (strpos($string, $value) !== false)
    {
        echo 'Matched value is ' . $value;
    }  
} 

回答by ArtisticPhoenix

Use this to get your numbers

使用它来获取您的号码

$re = "/(\d+)/";
$str = "abcdefg123hijklm";

preg_match($re, $str, $matches);

and ( 123 can be $matches[1] from above ):

和( 123 可以是上面的 $matches[1] ):

   preg_grep('/123/', $array);

http://www.php.net/manual/en/function.preg-grep.php

http://www.php.net/manual/en/function.preg-grep.php