从数组中删除与某个特定单词匹配的所有字符串(php) [英] Remove all strings from an array which match a certain word (php)

查看:62
本文介绍了从数组中删除与某个特定单词匹配的所有字符串(php)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组,想从数组中删除所有包含"Free_Course"一词的元素

I have an array of strings, and want to remove all elements from the array which contain the word "Free_Course"

该数组包含字符串,例如"Free_Course_123","Free_Course_124","Other_Course_123",Other_Course_1234等.

the array holds strings such as "Free_Course_123", "Free_Course_124", "Other_Course_123", Other_Course_1234 etc..

我只想遍历数组,并删除所有包含"Free_Course"的内容,因此这将摆脱上面列出的前2个元素.

I just want to loop through the array, and remove all containing "Free_Course", so this would get rid of the first 2 elements listed above.

我已经尝试过使用preg_grep功能,但到目前为止还没有成功:

I've tried using this preg_grep funciton, but had no success as of yet:

$cleanTags= preg_grep("/^Free_Course*/", $tags);

推荐答案

您尝试过

$tags = array(
    "Free_Course_123", 
    "Free_Course_124", 
    "Other_Course_123", 
    "Other_Course_1234"
);
$cleanTags= preg_grep("/Free_Course/", $tags, PREG_GREP_INVERT);
print_r($cleanTags);

?最后一个属性会反转过滤器功能,请参见手册

? The last attribute inverts the filter function, see the manual

我已经测试了结果,自PHP 4.2.0起应该可以使用

I have tested the result, it should work since PHP 4.2.0

Array ( [2] => Other_Course_123 [3] => Other_Course_1234 )

这篇关于从数组中删除与某个特定单词匹配的所有字符串(php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆