explode and in_Array search not working
OK here is the code codepad here http://codepad.org/ZQz0Kn3R
function processContent($content, $min_count = 2, $exclude_list = array()) {
$wordsTmp = explode(' ', str_replace(array('(', ')', '[', ']', '{', '}', "'", '"', ':', ',', '.', '?'), ' ', $content));
$words = array();
$wordsTmp2 = array();
$omit = array('and', 'or', 'but', 'yet', 'for', 'not', 'so', '&', '&', '+', '=', '-', '*', '/', '^', '_', '\\', '|');
if(count($exclude_list)>0){
$omit = array_merge($omit, $exclude_list);
}
foreach ($wordsTmp as $wordTmp) {
if (!empty($wordTmp) && !in_array($wordTmp, $omit) && strlen($wordTmp) >= $min_count) {
$words[] = $wordTmp;
}
}
return $words;
}
OK this is my function which should return array of words by filtering from $omit
variable. But when i use it the words in first $omit
array are only filtered, The second merged from the $exclude_list
is not filtered.
I use my function this way :
$filter_array = explode("\n", words list separated by \n new line here);
print_r(processContent('String gere for filtering', $min_word_length, $filter_array));
The variable $filter_array
is passed in to the exclude_list 开发者_Go百科also is merged to omit variable but does not get filtered in return value. only first $omit
value is filtered. Is there something wrong in code??
The problem is because $filter_array
has spaces in it. Either:
$filter_array = array_map(function($el) { return trim($el); }, $filter_array);
Or
foreach ($filter_array as &$element) {
$element = trim($element);
}
http://codepad.viper-7.com/BrZ9Rm
you will need to pass it through trim:
$filter_array = array_map(function($el) { return trim($el); }, $filter_array);
精彩评论