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 strlen, str_word_count, and substr_count functions:

// 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 echo:

// get length of example string
echo strlen($str); // 114

Our example string contains 114 characters.

Count the Number of Words in a String

The 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 str_word_count to echo and find that our example string contains 16 words.

The str_word_count function includes an optional second parameter.[1] Pass 1 or 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 print_r function:

// 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
(
    [0] => The
    [1] => letters
    [2] => PHP
    [3] => stand
    [4] => for
    [5] => PHP
    [6] => Hypertext
    [7] => Preprocessor
    [8] => PHP
    [9] => originally
    [10] => stood
    [11] => for
    [12] => Personal
    [13] => Home
    [14] => Page
    [15] => 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
(
    [0] => The
    [4] => letters
    [12] => PHP
    [16] => stand
    [22] => for
    [26] => PHP
    [31] => Hypertext
    [41] => Preprocessor
    [64] => PHP
    [68] => originally
    [79] => stood
    [85] => for
    [89] => Personal
    [98] => Home
    [103] => Page
    [108] => 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

The 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 

The substring '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

The substr_count function includes optional offset and 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 offset and 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.


  1. An optional third parameter allows you to provide a string containing additional characters that would be counted as a word by the str_word_count function. Find out more in the PHP Manual. ^