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

What Is a Progressive Web App? Why Would You Need One?

App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…

1 year ago

7 Most Popular Paid Online Advertising Strategy

As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…

1 year ago

The Importance of User-Centered Design in Mobile App Development

Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…

1 year ago

Healthcare Mobile App Development: A Complete Guide for Founders

COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…

1 year ago

Exploring Diverse WordPress Theme Niches: A Comprehensive Guide

Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…

1 year ago

8 Awesome Blog Content Ideas for Movers to Skyrocket the SEO

For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…

1 year ago