PHP str_word_count | Information About Words in a String

PHP str_word_count is an inbuilt function in PHP. It returns information about the words in a string on the basis of input format. Also, it is mostly used to calculate the number of words in a string. In this article, we will discuss the PHP str_word_count function. Also, we will discuss a few examples of using it.

For the purpose of this function, ‘word’ is defined as a locale dependent string containing alphabetic characters, which also may contain, but not start with “‘” and “-” characters.

https://www.php.net/manual/en/function.str-word-count.php

Syntax

str_word_count ( $string , $format = 0, $chars = null )

Parameters

The PHP str_word_count function expects three parameters. However, only one parameter is mandatory and two are optional. The description of the parameters is as below:

  • $string: The first parameter to the function is a string. It is a mandatory parameter.
  • $format: The second parameter to the function specifies the return format. It can contain three possible values:
    • 0: The function returns the number of words in the string. (Default)
    • 1: It returns an array containing all the words of the string.
    • 2: It returns an Associative Array with key as the position of word in the string and value as the word itself.
  • $chars: The third parameter specifies additional characters which the function will treat as words. Also, it is an optional parameter.

Return Value

The PHP str_word_count function returns an integer or an array depending on the input format to the function.

PHP str_word_count function

Examples

Let’s discuss a few examples of using the str_word_count function in PHP.

Example 1: Word Count

<?php 
	$string = 'Hello Wor4ld!';
	$numberOfWords = str_word_count($string);
  	echo $numberOfWords;
?> 

In the above example, although the input string contains 2 words, but the function returns 3 words because the integer 4 is by default not counted a part of an existing word. By default, the PHP str_word_count function only counts alphabetical characters as words. The output of the function is as follows:

OUTPUT:
3

Example 2: Getting Word List

Similarly, by passing the format parameter as 1, we can get an array of all the words in the string.

<?php 
  
	$string = 'Hello Wor4ld!';
	$words = str_word_count($string, 1);
  	print_r($words);
?> 

The function will return an array containing all the words.

OUTPUT:
Array
(
    [0] => Hello
    [1] => Wor
    [2] => ld
)

Example 3: Associative Array

Also, by passing 2 as the format parameter, we can get an associative array with key as word position and value as the word itself.

<?php 
  
	$string = 'Hello Wor4ld!';
	$words = str_word_count($string, 2);
  print_r($words);
?> 
OUTPUT:
Array
(
    [0] => Hello
    [6] => Wor
    [10] => ld
)
 

Example 4: Specifying Character List

Also, we can specify additional characters which we want to be counted as words in the third parameter.

<?php 
	$string = 'Hello Wor4ld!';
	$words = str_word_count($string, 1, '4!');
  	print_r($words);
?> 

In the above example, now the PHP str_word_count function will return an array containing two elements (2 words only). Now, the function also counts ‘4’ and ‘!’ as part of words.

OUTPUT:
Array
(
    [0] => Hello
    [1] => Wor4ld!
)
 

Conclusion

In this article, we discussed the PHP str_word_count function. You can read more about it on the Official PHP Documentation. Additionally, you can learn about more PHP String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *