Friday, April 20, 2012

php array still getting white space and nulls

trying to get two word phrases from an array but I keep getting one word phrases with a white space either before or after the word.



$text = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $text);
$textarray = explode(" ", $text);
array_filter($textarray);
$textCount = array_count_values($textarray);
arsort($textCount);
$twoWords = array();
for ($i = 0; $i<=count($textarray); ++$i) {
if ($textarray[$i]<>" ") {
$twoWords[$i] = $textarray[$i]." ".$textarray[$i + 1];
} else {
$twoWords[$i] = "blah";
}
}
foreach ($twoWordsCount as $tey => $val2) {
if ($tey == null || $tey == "" || $tey == "\r\n" || $tey == "\t" || $tey == "\r" || $tey == "\n" || $tey == "&nbsp;" || $tey == " " || $tey == " ") {
//do nothing
} else {
echo $val2."\n";
}
}


and for some reason this just returns values likes whitespace Hello or Test and then a whitespace, but I want it to return hello test





No comments:

Post a Comment