Skip to content

Instantly share code, notes, and snippets.

@nickabal
Created September 10, 2016 21:11
Show Gist options
  • Save nickabal/3c091446566d96d3619fd8b67ea9ac47 to your computer and use it in GitHub Desktop.
Save nickabal/3c091446566d96d3619fd8b67ea9ac47 to your computer and use it in GitHub Desktop.
given string of book count the words using php
<?php
$book = 'Some Text Is Here Text Is Text Text characters';
$book_words = explode(" ", $book);
$counted_words = array();
foreach ( $book_words as $word ) {
if ( array_key_exists($word, $counted_words) ) {
$counted_words[$word]++;
} else {
$counted_words[$word] = 1;
}
}
foreach ( $counted_words as $word => $count ) {
echo $word. " : ". $count. "<br>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment