Home  >  Article  >  Backend Development  >  怎么判断字符串中是否含有数组的某个元素

怎么判断字符串中是否含有数组的某个元素

WBOY
WBOYOriginal
2016-06-13 12:58:061002browse

如何判断字符串中是否含有数组的某个元素?

<br />
$check_array=array('官网','网站','地址','互联网');<br />
$string1="csdn的网站是csdn.net";<br />
$string2="今天天气不错";<br />


请问如何判断字符串中是否含有数组的某个元素。
返回结果“字符串1包含,字符串2不包含”

------解决方案--------------------
$check_array=array('官网','网站','地址','互联网');<br />
$string1="csdn的网站是csdn.net";<br />
$string2="今天天气不错";<br />
echo call_user_func('foo', $string1) ? '存在' : '不存在';<br />
echo '<br>';<br />
echo call_user_func('foo', $string2) ? '存在' : '不存在';<br />
function foo($str){<br />
    global $check_array;<br />
	$r=false;<br />
	foreach($check_array as $v){<br />
	    if(strpos($str,$v) !==false){ <br />
		   $r=true;<br />
		   break;<br />
		}   <br />
	}	<br />
	return $r;<br />
}

存在
不存在

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn