PHP str_word_count() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, str_word_count() counts the words in a string or returns them as an array.

A word is a run of letters that may also contain apostrophes and hyphens.

Syntax

Call str_word_count() with the values you need. Start with this line.

PHP
$count = str_word_count($text);
CallWhat it does
str_word_count("The quick brown fox")Returns 4.
str_word_count("The quick brown fox", 1)Returns an array of the four words.
str_word_count("The quick brown fox", 2)Returns an array keyed by each word's position in the string.

The format argument picks the return type: 0 for a count, 1 for a list of words, 2 for a list keyed by position.

Library declarations
PHP
str_word_count(string $string, int $format = 0, ?string $characters = null): array|int

Parameters

ParameterDescription
stringRequired. The string to examine.
formatOptional. 0 returns the word count, 1 returns an array of words, 2 returns an array keyed by position. Default is 0.
charactersOptional. Extra characters to treat as part of a word, for example digits.

Return Value

Return ValueDescription
intThe number of words when format is 0.
arrayAn array of words when format is 1 or 2.

PHP Examples

Example 1 — Count Words

The simplest use: how many words are in this sentence?

PHP
<?php
$text = "The quick brown fox";

echo str_word_count($text), PHP_EOL;

How It Works

  1. Create a sentence. Words are separated by spaces.
  2. Call the function. With the default format of 0 it returns a count.
  3. Get an integer. The result is 4.
  4. Print it. Use echo to display the number.

Example 2 — Return Words as an Array

Use format 1 to list the words. Apostrophes and hyphens stay inside a word.

PHP
<?php
$words = str_word_count("Don't stop-me now", 1);

print_r($words);

How It Works

  1. Set format to 1. The function returns an array instead of a number.
  2. Apostrophe kept. Don't stays as one word.
  3. Hyphen kept. stop-me is one word, too.
  4. Print the array. print_r() shows each index and word.

Example 3 — Count Numbers as Words

Digits are not word characters by default. Pass them in the third argument to include them.

PHP
<?php
$text = "room 101 and 202";

echo str_word_count($text), PHP_EOL;
echo str_word_count($text, 0, "0123456789"), PHP_EOL;

How It Works

  1. Default count. Only room and and are words, so the result is 2.
  2. Add digits. The third argument tells PHP that 0 to 9 count as word characters.
  3. Recount. Now 101 and 202 are words, too.
  4. New total. The second call returns 4.

Frequently Asked Questions

str_word_count() counts the words in a string. It can also return the words as an array.
array|int str_word_count(string $string, int $format = 0, ?string $characters = null). Only the string is required.
0 returns the number of words, 1 returns an array of the words, and 2 returns an array whose keys are the position of each word in the string.
By default only letters, apostrophes, and hyphens count. Pass digits like "0123456789" as the third argument to include them.
Not well. It uses simple letter rules, so accented or non-Latin words may be split. Use preg_split() or mb functions for Unicode text.

Next: strcasecmp()

strcasecmp() compares two strings without caring about upper or lower case.

strcasecmp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful