str_word_count() function will counts the number of words in a string.
This will return a number or an array, depending upon the returnformat parameter value.
[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, Description
PHP 5.1.0 , The characterlist parameter was added.
[/table]
<?php $strExample = "Hi from tutorialmines."; echo str_word_count($strExample); ?>
Output of above code in the browser is as below:
<?php $strExample = "Hi from tutorialmines."; print_r(str_word_count($strExample,1)); ?>
Output of above code in the browser is as below:
<?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 )
<?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 )
When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…
In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…
In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…
In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…
When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…
In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…