Home >Backend Development >PHP Tutorial >php排除字符串里包含某字符,该如何处理

php排除字符串里包含某字符,该如何处理

WBOY
WBOYOriginal
2016-06-13 11:48:10850browse

php排除字符串里包含某字符

$a = "'你好.你好','你好啊ct','他好','他好啊','我好','我好啊'";



怎么把$a里包含"."和"ct"的排除掉


就是把'你好.你好''你好啊ct'整个排除掉

得到结果$b = "'他好','他好啊','我好','我好啊'";

麻烦知道的大哥直接给完整代码

最好是要排除的包含关键字自己可以添加
类似$c = array(".", "ct");
我可以在$c里添加要排除的包含字符

------解决方案--------------------
$a = "'你好.你好','你好啊ct','他好','他好啊','我好','我好啊'";
$b=explode(',',$a);
$c = array(".", "ct");
foreach($b as $value){
foreach($c as $v){
if(strpos($value,$v) !==false){
$ar[]=$value;
break;
}
}
}
$result = array_diff($b, $ar);
echo join(',',$result);

------解决方案--------------------
或者这样
 
$a = "'你好.你好','你好啊ct','他好','他好啊','我好','我好啊'";
$b=explode(',',$a);
$c = array(".", "ct");
foreach($c as $v){
$p="/" .preg_quote($v) ."/";
$ar[]=array_values(preg_grep($p,$b));
}
foreach($ar as $v) $arr[]=$v[0];
$result=array_diff($b,$arr);
echo join(',',$result);

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