如何检查 a 是否$string
包含数组中表示的任何项目?
$string = 'My nAmE is Tom.';
$array = array("name","tom");
if(contains($string,$array))
{
// do something to say it contains
}
有任何想法吗?
我不认为有一个内置函数可以处理你想要的。但是,您可以轻松编写一个contains()
函数:
function contains($str, array $arr)
{
foreach($arr as $a) {
if (stripos($str,$a) !== false) return true;
}
return false;
}
那是你想要的吗?我希望代码正在编译:)
$string = 'My nAmE is Tom.';
$array = array("name","tom");
if(0 < count(array_intersect(array_map('strtolower', explode(' ', $string)), $array)))
{
//do sth
}
使用公认的答案:
$string = 'My nAmE is Tom.';
$array = array("name","tom");
if(0 < count(array_intersect(array_map('strtolower', explode(' ', $string)), $array)))
{
//do sth
}
请注意,if 语句可以更改为:
if(0 < count(array_intersect(explode(' ', strtolower($string)), $array)))
因为实际上没有必要使用 array_map 来应用于strtolower
每个元素。而是将其应用于初始字符串。
contains 函数的另一种解决方法
function contains($string, $array, $caseSensitive = true)
{
$stripedString = $caseSensitive ? str_replace($array, '', $string) : str_ireplace($array, '', $string);
return strlen($stripedString) !== strlen($string);
}
PS。至于我,我只是在没有功能的情况下使用它..
if (strlen(str_replace($array, '', $string)) !== strlen($string)) {
// do it
}
像这样的东西会起作用:
$string = 'My nAmE is Tom.';
$array = array("name", "tom");
foreach ($array as $token) {
if (stristr($string, $token) !== FALSE) {
print "String contains: $token\n";
}
}
我们可以检查给定字符串中是否存在数组的任何元素。
$string = 'My nAmE is Tom.';
$array = array("name","tom");
if(str_replace($array, '', strtolower($string)) !== strtolower($string)) {
// If String contains an element from array
// Do Something
}
<?php
$input = preg_quote('blu', '~'); // don't forget to quote input string!
$data = array('orange', 'blue', 'green', 'red', 'pink', 'brown', 'black');
$result = preg_grep('~' . $input . '~', $data);
print_r($result);
?>
这会做这项工作吗?
$words = explode(" ", $string);
$wordsInArray = array();
foreach($words as $word) {
if(in_array($word, $array)) {
$wordsInArray[] = $word;
}
}
function contains($str, $arr)
{
$ptn = '';
foreach ($arr as $s) {
if ($ptn != '') $ptn .= '|';
$ptn .= preg_quote($s, '/');
}
return preg_match("/$ptn/i", $str);
}
echo contains('My nAmE is Tom', array('name', 'tom'));
使用 array_intersect() 函数的另一种方法,试试下面的代码:
function checkString(array $arr, $str) {
$str = preg_replace( array('/[^ \w]+/', '/\s+/'), ' ', strtolower($str) ); // Remove Special Characters and extra spaces -or- convert to LowerCase
$matchedString = array_intersect( explode(' ', $str), $arr);
if ( count($matchedString) > 0 ) {
return true;
}
return false;
}
有更简单的方法
$string = 'My nAmE is Tom.';
$convert=explode(" ",$string,5);
if(in_array("My", $convert)){
echo "Ja";
}else{
echo "Nein";
}
我做了一些测试,因为我需要根据我们不允许的单词列表检查用户输入。
我发现将所有内容都转换为小写(因为我的列表是小写的)然后使用数组相交是迄今为止最快的。
**First Option I Tested**
$tempString= explode(' ',strtolower($string));
$foundWords = array_intersect($tempString,$profanities);
Time taken: 0.00065207481384277
**The second option I tested**
$tempWords = explode(' ',$words);
foreach ($tempWords as $word)
{
foreach ($profanities as $profanity)
{
if (stripos($word,$profanity) !== false) return true;
}
}
Time Taken: 0.024131059646606
由于我必须获得匹配计数,因此我使用了 preg_match_all
preg_match_all("/" . implode(' | ', $array) . "/i", " ".str_replace([':', '-'], ' ',$string)." ", $matches);
count($matches[0]);
更简单,请参考链接in_array
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Es Irix";
}