从 PHP 数组中删除选定单词的有效方法


Efficient ways to remove select words from a PHP Array

我创建了一个数组来获取文件,然后解析该文件的内容。 我已经用if(strlen($value) < 4): unset($content[$key]); endif;过滤掉了少于 4 个字符的单词

我的问题是这个 - 我想从数组中删除常用词,但其中有很多。 与其对每个数组值一遍又一遍地进行这些检查,我想知道是否有更有效的方法可以做到这一点?

这是我当前使用的代码示例。 这个列表可能很大,我认为必须有更好(更有效(的方法?

foreach ($content as $key=>$value) {
    if(strlen($value) < 4): unset($content[$key]); endif; 
    if($value == 'that'): unset($content[$key]); endif;
    if($value == 'have'): unset($content[$key]); endif;
    if($value == 'with'): unset($content[$key]); endif;
    if($value == 'this'): unset($content[$key]); endif;
    if($value == 'your'): unset($content[$key]); endif;
    if($value == 'will'): unset($content[$key]); endif;
    if($value == 'they'): unset($content[$key]); endif;
    if($value == 'from'): unset($content[$key]); endif;
    if($value == 'when'): unset($content[$key]); endif;
    if($value == 'then'): unset($content[$key]); endif;
    if($value == 'than'): unset($content[$key]); endif;
    if($value == 'into'): unset($content[$key]); endif;
}
这是我

的做法:

$exlcuded_words = array( 'that','have','with','this','your','will','they','from','when','then','than','into');
$replace = array_fill_keys($exlcuded_words,'');
echo str_replace(array_keys($replace),$replace,'some words that have to be with this your will they have from when then that into replaced');

它的工作方式:创建一个数组,充满空字符串,其中键是您要删除/替换的子字符串。 Just Use str_replace ,将键作为第一个参数传递,数组本身作为第二个参数传递,在这种情况下的结果是:some words to be replaced .此代码已经过测试,工作正常。

在处理数组时,只需用一些古怪的分隔符(如%@%@%或其他东西(将其内爆,然后str_replace批次,再次爆炸该批次,Bob 就是你的叔叔


当谈到用少于 3 个字符替换所有单词时(我在原始答案中忘记了(,这是正则表达式擅长的事情......我会说类似preg_replace('('b|[^a-z])[a-z]{1,3}('b|[^a-z])/i','$1$2',implode(',',$targetArray));之类的话。
你可能想测试一下这个,因为这只是我的头顶,未经测试。但这似乎足以让你开始

也许这会更好:

$filter = array("that","have","with",...);
foreach ($content as $key=>$value) {
   if (in_array($value,$filter)){
      unset($content[$key])
   }
}

我可能会做这样的事情:

$aCommonWords = array('that','have','with','this','yours','etc.....');
foreach($content as $key => $value){
    if(in_array($value,$aCommonWords)){
        unset($content[$key]);
    }
}

创建一个要删除的单词数组,并检查该值是否在该数组内

$exlcuded_words = array( 'that','have','with','this','your','will','they','from','when','then','than','into');

如果foreach

if (in_array($value, $excluded_words)) unset($content[$key];

另一种可能的解决方案:

$arr = array_flip(array( 'that', 'have', 'with', 'this', 'your', 'will', 
        'they', 'from', 'when', 'then', 'than', 'into' ));
foreach ($content as $key=>$value) {
    if(strlen($value) < 4 || isset($arr[$value])) {
        unset($content[$key]);
    }
}

使用 array_diff()

$content = array('here','are','some','words','that','will','be','filtered');
$filter = array('that','have','here','are','will','they','from','when','then');
$result = array_diff($content, $filter);

结果:

Array
(
    [2] => some
    [3] => words
    [6] => be
    [7] => filtered
)
或者,如果您希望在过滤

内容方面具有更大的灵活性(例如,您提到需要过滤掉少于 4 个字符的单词(,您可以使用 array_filter()

$result = array_filter($content, function($v) use ($filter) {
    return !in_array($v, $filter) && strlen($v) >= 4;
});

结果:

Array
(
    [2] => some
    [3] => words
    [7] => filtered
)
$var = array('abb', 'bffb', 'cbbb', 'dddd', 'dddd', 'f', 'g');
$var= array_unique($var);
foreach($var as $val){
    echo $val. " ";
}

结果:

abb
bffb
cbbb
dddd
f
g

最简单的方法