php

PHP str_word_count() Function

PHP has many built-in functions to work with string data. The str_word_count() is one of the built-in string functions of PHP to count the total number of words that exist in a string data or create an array by splitting the string data based on the space. The syntax of this function is given below.

Syntax:

array|int str_word_count(string $string [, int $format = 0 [, string $characters = null]])
  • The first argument is mandatory, which takes a string value, and the function will be applied to this string.
  • The second argument is optional, which is used to define the format type, and the output of the function depends on the format value. This argument can take any of the following three values.

0: It returns the number of words in the string data.

1: It returns an array containing all the words that exist in the string.

2: It is used to return an associative array where the key of the array will be the numerical position of the word, and the value of the array will be each word of the string data.

  • The third argument is optional, used to define the additional characters.
  • The function will return an array if the second argument contains 1 or 2; otherwise, the function will return an integer value.

str_word_count() Function Examples

The uses of str_word_count() function have been shown in the next part of the tutorial by using multiple examples.

Example-1: Use of str_word_count() without optional arguments

Create a PHP file with the following script that will count the total number of words in a string by using the str_word_count() function without the optional argument. A string data of 6 words has been used in the script.

<?php

//Declare the string data

$strdata = "Learn PHP programming from the basic";

//Print the string data

echo "The string data is: <br/>".$strdata."<br/>";

//Count and print the total number of words in the string data

echo "<br/>Total words in the string is: ".str_word_count($strdata);

?>

Output:

The following output will appear after executing the above script.

Example-2: Create Numeric Array From the String Data

Create a PHP file with the following script that will create an array with each word of the string by using the str_word_count() function with the optional argument value, 1. The index value of the array will be started from 0 like the numeric array. A string data of 6 words has been used in the script.

<?php

//Declare the string data

$strdata = "Learn PHP programming from the basic";

//Print the string data

echo "The string data is: <br/>".$strdata."<br/>";

echo "<br/>The output of the numeric array with the formatting:<br/>";

echo "<pre>";

//Print the numeric array generated from the string data

print_r(str_word_count($strdata, 1));

echo "</pre>";

?>

Output:

The following output will appear after executing the above script.

Example-3: Create an Associative Array From the String Data

Create a PHP file with the following script to create an array with each string word using the str_word_count() function with the optional argument value, 2. Each index of the array will contain the position of each array value in the string. A string of data of 6 words has been used in the script.

<?php

//Declare the string data

$strdata = "Learn PHP programming from the basic";

//Print the string data

echo "The string data is: <br/>".$strdata."<br/>";

echo "<br/>The output of the associative array with the formatting:<br/>";

echo "<pre>";

//Print the associative array generated from the string data

print_r(str_word_count($strdata, 2));

echo "</pre>";

?>

Output:

The following output will appear after executing the above script.

Example-4: Create a Numeric Array by Using Additional Characters

Create a PHP file with the following script that will show the purpose of using the third argument of the str_word_count() function. The first str_word_count() function used in the script will return an array-like example 2 that will omit the numeric values of the string data. The second str_word_count() function used in the script will return an array that will contain the numeric value for using the additional characters.

<?php

//Declare the string data

$strdata = "Learn PHP 8.0 from the basic";

//Print the string data

echo "The string data is: <br/>".$strdata."<br/>";

echo "<br/>The output of the array with the formatting:<br/>";

echo "<pre>";

//Print the numeric array generated from the string data

print_r(str_word_count($strdata, 1));

//Print the numeric array generated from the string data with the additional characters

print_r(str_word_count($strdata, 1, '8.0'));

echo "</pre>";

?>

Output:

The following output will appear after executing the above script.

Example-5: Use of str_word_count() Function Inside a Function

Create a PHP file with the following script to search a particular word in a string by using the str_word_count() function inside a user-defined function. The string value and search value will be passed as the argument values of the user-defined function. An array will be created from the string value by using the str_word_count() function, and the foreach loop has been used to iterate the array values to find out whether the search value exists in the array or not.

<?php

/*

Define the function to search

a particular word in the string data

by using str_word_count() function

*/


function search_word($text, $search) {

//Split the string data based on space

$strarr=str_word_count($text,1);

//Initialize the variable

$found = 0;

//Search the particular value in the array

foreach($strarr as $value )

{

if($search == $value)

{

$found = 1;

break;

}

}

//Check the search value is found or not

if($found == 1)

echo "Found.";

else

echo "Not Found.";

}

//Initialize the string value

$strdata = "Welcome to LinuxHint";

//Initialize the search value

$search = "to";

echo "The string value is: <b> $strdata </b><br/>";

echo "The search value is: <b> $search </b><br/>";

//Call the function to search a word in a string

search_word($strdata, $search);

?>

Output:

The following output will appear after executing the above script.

Conclusion

Different ways of using the str_word_count() function for different purposes have been shown in the examples of this tutorial to help the PHP users to know the use of this function properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.