Home > php教程 > PHP源码 > body text

PHP循环判断字符串是否包含数组的值

WBOY
Release: 2016-06-08 17:21:36
Original
1390 people have browsed it

下面来看一个利用foreach遍历数组之后再判断数组值是否在字符串中出现过,具体操作代码如下图所示。

<script>ec(2);</script>

例子

 代码如下 复制代码

function is_exist($str,$key){
 foreach($key as $v){
  if(strpos($str,$v)>-1){
   return true;
  }
 }
 return false;
}
$str = 'abcdefg我们中国';
$key = array('4','5','a');
if(is_exist($str,$key)){
 echo 'YES';
}
else{
 echo 'NO';
}
?>

原理分析

很简单的一个例子我们只要使用foreach遍历数组的值之后再使用strpos($str,$v)>-1来判断数组值是不是在字符串中指定位置了,如果搜索到了就会返回大于或等于0值哦。

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!