Get String Length, Word Count, Substring Count
PHP provides functions that count how many characters or words are contained in a string or how many times a particular substring occurs in a string. We will use the following string to demonstrate PHP's
// example string $str = 'The letters PHP stand for PHP: Hypertext Preprocessor. PHP originally stood for Personal Home Page Tools.';
Get String Length
Pass a string to the
strlen function and it will count the number of characters in that string and return that count. We demonstrate by passing the string declared above to the
strlen function and display the result using
// get length of example string echo strlen($str); // 114
Our example string contains 114 characters.
Count the Number of Words in a String
str_word_count function, when passed a single string argument, returns a count of the number of words in that string:
// count words in the above example string
echo str_word_count($str); // 16
We pass the return value of
echo and find that our example string contains 16 words.
str_word_count function includes an optional second parameter. Pass
2 as the second argument and the function will return an array. Let's try it first with a
1, and output the result using the
// pass 1 as second argument to str_word_count print_r( str_word_count($str, 1) ); /* print_r output (as seen in page source view): Array (  => The  => letters  => PHP  => stand  => for  => PHP  => Hypertext  => Preprocessor  => PHP  => originally  => stood  => for  => Personal  => Home  => Page  => Tools ) */
We can see that the resulting array is numerically indexed with each word occupying a slot in the array. Now let's try passing
2 as the second argument:
// pass 2 as second argument to str_word_count print_r( str_word_count($str, 2) ); /* print_r output (page source view): Array (  => The  => letters  => PHP  => stand  => for  => PHP  => Hypertext  => Preprocessor  => PHP  => originally  => stood  => for  => Personal  => Home  => Page  => Tools ) */
Notice that in this array the key is the index position of the word in the string while the value is the word itself.
Count Substring Occurrences in String
substr_count function counts the number of times that a substring occurs in a string. Let's see how many times the substring
'PHP' occurs in the example string defined at the top of the page:
echo substr_count($str, 'PHP'); // 3
'PHP' occurs 3 times in the example string. The
substr_count function is case sensitive. If you were to search for
'php', the result would be 0.
Offset and Length Parameters
substr_count function includes optional
length parameters. We will demonstrate the
offset option first by commencing our search at the location of the first period in our example string. We use the
strpos function to locate it:
/* substr_count arguments: * subject string, search string, offset (optional), length (optional) */ // count instances of 'PHP' in $str from first period echo substr_count($str, 'PHP', strpos($str, '.') ); // 1
As you can see, only one instance of the string
'PHP' occurs in the second sentence of our example string.
Our next example makes use of both the
length parameters. We want to determine how many instances of the substring
'PHP' occur in the first sentence of our example string. We pass
0 to start our count at the beginning of the string, and we use the
strpos function to calculate the location of the first period.
// count instances of 'PHP' in $str before first period echo substr_count($str, 'PHP', 0, strpos($str, '.') ); // 2
We find two instances of
'PHP' in the first sentence of our example string.
Find out more about
strpos in our presentation on PHP Search Functions.
The count_chars Function
PHP provides another counting function:
count_chars, which provides information about the characters in your strings. Find out more about it in the PHP Manual.