str_word_count – PHP String Functions

Syntax :

str_word_count (string, returnformat, characterlist );

Description :

str_word_count() function will counts the number of words in a string.


Parameter :

  • string – This is a Required parameter. It is the input string on which function will be performed.
  • returnformat – This is a Optional parameter. It tells us about the return value of the str_word_count() function. Below are the values which can be pass in this parameter. 0 is the default value.
    • 0 – It is the default value. This will return number of words count in a string.
    • 1 – This will return an array with the words from the string.
    • – returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself
  • characterlist  – This is an Optional parameter. We can set special characters, which will be consider in word count.

Output :

This will return a number or an array, depending upon the returnformat parameter value.


ChangeLog :

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, Description
PHP 5.1.0 , The characterlist parameter was added.

[/table]


Related articles : explode(), split() ,count_chars(), substr_count(), substr_replace().


str_word_count() – PHP Functions Example 1 : By default its returnformat is 0, which will return  word count in a string.
<?php
$strExample = "Hi from tutorialmines."; 
echo str_word_count($strExample);
?>

Output of above code in the browser is as below:

3

str_word_count() – PHP Functions Example 2 : When returnformat is set to 1, It will return  an array with the words from the string.
<?php
$strExample = "Hi from tutorialmines."; 
print_r(str_word_count($strExample,1));
?>

Output of above code in the browser is as below:

Array ( [0] => Hi [1] => from [2] => tutorialmines )

str_word_count() – PHP Functions Example 3 : When returnformat is set to 2, It will return an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself
<?php
$strExample = "Hi from tutorialmines."; 
print_r(str_word_count($strExample,2));
?>

In above example ,We have a string “Hi from tutorialmines.”. Now this function will pad the string “!” in the LEFT and RIGHT both side of string and makes its length to 30. Output of above code in the browser is as below:

Array ( [0] => Hi [3] => from [8] => tutorialmines )


str_word_count() – PHP Functions Example 4 : length value is less than string length.
<?php
$strExample = "Hello & Hi from tutorialmines web * site."; 
echo "WithOut Charlist param<br />";
print_r(str_word_count($strExample,1));
echo "<br />With Charlist param set to '&'<br />";
print_r(str_word_count($strExample,1,'&'));
echo "<br />With Charlist param set to '&' and '*'<br />";
print_r(str_word_count($strExample,1,'&*'));
?>

In above example ,We have a string “Hello & Hi from tutorialmines web * site.”. Output of above code in the browser is as below:

WithOut Charlist param
Array ( [0] => Hello [1] => Hi [2] => from [3] => tutorialmines [4] => web [5] => site )
With Charlist param set to ‘&’
Array ( [0] => Hello [1] => & [2] => Hi [3] => from [4] => tutorialmines [5] => web [6] => site )
With Charlist param set to ‘&’ and ‘*’
Array ( [0] => Hello [1] => & [2] => Hi [3] => from [4] => tutorialmines [5] => web [6] => * [7] => site )

You may also like...

Leave a Reply

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