PHP Questions Home | Register | Login
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website
Unanswered

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

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";
?>



Comment on this Question and Answer >>>

ASK A QUESTION

More strings PHP Questions

What is the difference between print and echo?

How do I remove trailing whitespace?

How do you hash the password in this php script: define(sb db user name, admin store); define(sb db password, mypassword);

How can I remove spaces from around a string value?

How do I count how many times each character occurs in a string?



Custom Search