PHP: Find Length and Word Count of String

Posted April 11, 2013 by Chris Luongo in PHP programming

Understanding PHP’s numerous built-in functions for manipulating strings is essential if you ever want to be efficient with the language.

We manipulate variables holding strings in PHP through a variety of functions. PHP contains over 100 functions for dealing with strings, as a huge chunk of the work you will do on the web will evolve strings.

Learning how some of these functions work may seem tedious, but it is important to know. You may find yourself later trying to build a web app to do something very simple, yet without this knowledge, you will not be able to figure out how. We will take a look at two PHP functions for counting a string’s letters and words.

Find the length of a string

To find the length of a string, we can use the php function below:

strlen()

This function can take a string variable as an argument and return the number of characters in the string. I could do this:


$str = "Microsoft";
echo strlen($str);

In the code above, we set the variable str to hold the text Microsoft. Then using the strlen
function, we pass str into the function and have it echo out the result. We would see 5 returned if this were run.

This can be helpful if you were building a form application and need to see if a user’s password meets a certain word length requirement. In the following code, I created a simple if statement to check if a user’s password meets the length requirements of seven characters:


if (strlen($password) != 7) {
	echo "Your password must contain at least 7 or more characters";
} else {
	//Create user password
}

Assume $password will contain our users entered data, and we can validate this data and then make a decision based on it. This is very useful!

Find the number of words in a string

Another useful function similar to strlen is one that will return the number of words in a string. This one is called:


str_word_count()

We would simply pass in some text or a string variable into the function to get an answer returned:


$str = "This is a string of text and words";
echo str_word_count($str);

This would return 8 since the string $str contains eight words.

 

About Chris Luongo

Chris is a self-taught web designer and developer out of Atlanta, GA. This geek enjoys coffee, cold brews, bike riding, and twisting any form of tech into submission.
View more articles by Chris Luongo

The Conversation

Follow the reactions below and share your own thoughts.

  • DHall

    PHP: Find Length and Word Count of String

    To find the length of a string, we can use the php function below:

    strlen()

    This function can take a string variable as an argument and return the number of characters in the string. I could do this:

    $str = “Microsoft”;
    echo strlen($str);

    In the code above, we set the variable str to hold the text Microsoft. Then using the strlen
    function, we pass str into the function and have it echo out the result. We would see 5 returned if this were run.

    It should say: “We would see 9 returned if this were run”.