2012-08-04 62 views
2

我创建了一个数组来获取文件,然后解析该文件的内容。我已经过滤掉了不足4个字符的字词if(strlen($value) < 4): unset($content[$key]); endif;删除PHP数组中的选择字的有效方法

我的问题是这样的 - 我想从数组中删除常用单词,但其中有很多。我不知道是否有一种更有效的方法来做到这一点,而不是一遍又一遍地重复每个数组的值。

下面是我目前使用的代码示例。这个清单可能很大,我想这必须有一个更好(更有效率)的方式?

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; 
} 
+0

上帝,我希望如此。 – 2012-08-04 22:57:13

+0

至少,我建议在第一次检查后使用'elseif'进行检查。如果'$ value =='那'',则不需要继续检查其他可能的值。如答案中所示,有更好的方法来处理这个问题;我只想指出一些你可能能够应用到你写的其他代码的东西。 – 2012-08-04 23:31:15

回答

2

以下是我会做:

$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'); 

它的工作方式:使一个数组,全空的字符串,键是您想要删除/替换的子字符串。正好使用str_replace,传递键作为第一个参数,数组本身作为第二个参数,结果在这种情况下是:some words to be replaced。这段代码已经过测试并且工作得很好。

当处理数组,只需用一些古怪的分隔符爆它(如%@%@%或东西)和str_replace了很多,再次爆炸的很多,鲍勃是你的叔叔


当涉及到更换所有文字少于3个字符(我在我的原始答案中忘了),这是正则表达式擅长的东西...我会说类似preg_replace('(\b|[^a-z])[a-z]{1,3}(\b|[^a-z])/i','$1$2',implode(',',$targetArray));或类似的东西。
你可能想要测试这一个,因为这只是我的头顶,并没有经过测试。但这似乎足以让你开始

2

这也许会更好:

$filter = array("that","have","with",...); 

foreach ($content as $key=>$value) { 
    if (in_array($value,$filter)){ 
     unset($content[$key]) 
    } 
} 
+2

而不是'in_array',IMO最好使用'array_key_exists'或'isset($ filter [$ content])''。它快于'in_array',结果几乎相同 – 2012-08-04 23:16:03

1

我可能会做这样的事情:

$aCommonWords = array('that','have','with','this','yours','etc.....'); 

foreach($content as $key => $value){ 
    if(in_array($value,$aCommonWords)){ 
     unset($content[$key]); 
    } 
} 
1

使您要删除和查询词的数组,如果该值在该阵列内

$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]; 
+0

真棒 - 感谢您的快速响应! – Drazion 2012-08-04 23:06:28

+0

我会重新考虑删除所有少于3个字母的单词。什么与缩写或收缩 – Zefiryn 2012-08-04 23:09:39

0

另一种可能的解决方案:

$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]); 
    } 
} 
相关问题