php – preg_replace with pattern和subject数组返回空数组
发布时间:2020-12-13 17:43:41 所属栏目:PHP教程 来源:网络整理
导读:我想要一个简单的方法从$keywords中删除$badwords的元素. 我有什么(作为一个例子) $keywords = array('sebastian','nous','helene','lol'); //it could be anything in fact$badwords = array('nous','lol','ene','seba'); //array full of stop words that
我想要一个简单的方法从$keywords中删除$badwords的元素.
我有什么(作为一个例子) $keywords = array('sebastian','nous','helene','lol'); //it could be anything in fact $badwords = array('nous','lol','ene','seba'); //array full of stop words that I won't write here $filtered_keywords = array_filter(preg_replace($badwords,'',$keywords),'strlen'); print_r($filtered_keywords); 我的期望 Array ( [0] => samaha [1] => helene ) 我得到了什么 Sweet nothing :) 我试图使用str_ireplace,但它变坏了,因为它正在替换我的数组中的字符串. 解决方法
使用array_diff
var_dump(array_diff($keywords,$badwords)); array(2) { [0]=> string(9) "sebastian" [2]=> string(6) "helene" } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |