<?
//只输出一个关键字
$arr="判断,国,字符串,PHP"; //关键字
$text="我有困有国有要有判断有要有"; //标题
echo check_in($arr, $text);
function check_in($arr, $text){
//拆分字符串为数组
$keys = explode(',',$arr);
$result = $text; //如果不包含则
if($keys){
foreach($keys as $key){
if(strstr($text,$key)!=''){
$result = $key; //如果有则输出关键字
break;
}
}
}
return $result;
}
?>
<?php
//可以输出多个关键字
$keywords="有,国,字符串,PHP"; //关键字
$text="我有困有国有要有判断有要有"; //标题
//首页拆分字符串为数组,(回调函数过滤数组中的元素)
$keywords_arr=array_filter(explode(',',$keywords));
//循环遍历数组 每进行一次循环,当前数组元素的值就会被赋值给$eachkeyword
foreach($keywords_arr as $eachkeyword){
//判断是否包含,如果为true
if(stripos($text,$eachkeyword)!==false){
//则输出关键字
echo $eachkeyword;
}
}
?>
转载于:https://my.oschina.net/u/1046828/blog/176974