Skip to content
Tutorials Center
My learning

Topics

PHP Reference · String Functions

PHP str_word_count() function

Reference for PHP str_word_count(): count the words in a string or list them with positions, what counts as a word, extra characters and UTF-8 limits.

str_word_count() counts the words in a string, or returns them as an array. A word, for this function, is a run of letters that may also contain an apostrophe or a hyphen. Everything else (digits, punctuation, spaces) separates words.

The definition of "letter" depends on the current locale and is limited to single-byte characters. For English text the function works well. For text with accents or non-Latin scripts it splits words at the wrong places, and you should use a regular expression with the u modifier or IntlBreakIterator instead.

PHP
<?php
echo str_word_count("The quick brown fox");
// 4

Try it

PHP Counting and listing words

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Limits with accented text and a UTF-8 alternative

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
str_word_count(string $string, int $format = 0, ?string $characters = null): array|int
Name Description
$string The text to analyze.
$format 0 returns the number of words; 1 returns an array of the words; 2 returns an associative array whose keys are the byte positions where each word starts.
$characters Extra characters to treat as part of a word. A range can be written with .., for example "0..9".

Return value: an int for format 0, otherwise an array.

Errors: a ValueError if $format is not 0, 1 or 2.

Usage notes

  • What is a word. Letters, plus ' and - inside a word. A hyphen or apostrophe at the start of a word, or a hyphen at its end, is not counted as part of it.
  • Digits split words. "abc123def" has two words. Add "0..9" as $characters to include digits.
  • Single-byte only. Bytes above 127 are treated according to the locale, which in a default PHP setup means they are not letters, so accented characters break words apart. For UTF-8 use preg_match_all('/[\p{L}\p{M}\'-]+/u', ...).
  • Byte positions. With format 2, keys are byte offsets, not character offsets.
  • Not a language-aware word counter. Chinese, Japanese and Thai have no spaces between words; use a segmentation library or IntlBreakIterator.
  • Cheaper alternative. For a very rough count of whitespace-separated tokens, count(preg_split('/\s+/', trim($s), -1, PREG_SPLIT_NO_EMPTY)) treats "don't", "3.14" and "C++" as single words.
  • Estimating reading time. Divide the count by about 200 words per minute and round up.