PHP Questions Home

Categories

Arrays
Files
Forms
Functions
Images
MySQL
Numbers
Others
Strings
Website


PHP Functions

PHP Functions


More PHP

Top Questions
Ask a Question

How do I count the number of words in my text?


A common query is to be able to count the number of words in a piece of text or prose with PHP.

There are a few ways of doing this, a simple way would be to explode the string into an array on the space character, and then see how many entries there are in the array, corresponding to words separated by spaces.

Here's a snippet of code to do just this for you which should serve your needs:

<?php
$text 
"I wrote a really long piece of text but I got bored after a little while but this should still give you an example of what this does and how it works";
$words explode(" "$text);
$wordcount count($words);
echo 
"It seems to me that there are $wordcount words in this snippet of text";
?>



More strings PHP Questions

How do I turn a string into an array?
How can I remove trailing space from a string?
How do I make sure a string is a certain length?
How do I make the first letter of each word upper case?
How do I convert relevant characters in a string to HTML entities?