Categories: PHP

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.
    • 2 – 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 )

jyoti rani

Recent Posts

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

3 weeks ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

3 weeks ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

3 weeks ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

3 weeks ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

3 weeks ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

4 weeks ago