string - PHP how to count the length of each word in the text file -
i need help. how count length of each word in text file using php.
for example. there test.txt. , contain " hello everyone, need help." how output text , count length of each word,like:
array
hello => 5 => 8 => 1 need => 4 => 4 => 4
i start learn php. please explain detail code write.
many thanks
if don't need process words' length later, try this:
// file contents $text = file_get_contents('path/to/file.txt'); // break text array of words $words = str_word_count($text, 1); // display text echo $text, '<br><br>'; // , every word it's length foreach ($words $word) { echo $word, ' => ', mb_strlen($word), '<br>'; }
but noticed, str_word_count()
function has many issues utf-8 string (f.e. polish, czech , similar characters). if need those, suggest filtering out commas, dots , other non-word characters , using explode()
$words
array.
Comments
Post a Comment