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 )
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…