How to use str word count in PHP

In this article I will explain how the str_word_count() function can be used in PHP.
  • 2084

str_word_count() function in PHP

  • The str_word_count() function is used to count the number of words in a string.
  • The str_word_count() function returns an array or an integer, depending on the format chosen.

Syntax

str_word_count(string,return,char)

Parameter

  • string string is required parameter. it is specify for check to the string.
  • return return is optional parameter. it is specify the return value of the str_word_count() function. Possible values:
    • 0 - This is the default value. it is return the number of words found.
    • 1 - it is return an array with the words from the string.
    • 2 - it is return an array where the key is the position of the word in the string, and value is the actual word.
  • char char is optional parameter. it is specify for special characters to be considered as words.
Example

The following example show to how the str_word_count() function can be used in PHP.

<html>

<body>

<h3 style="color: darkgreen;">str_word_count() function example in PHP</h3>

    <?php

    $string='Welcome to C-sharpcorner';

    print_r(str_word_count($string,0));

    echo '<br>';

    print_r(str_word_count($string,1));

    echo '<br>';

    print_r(str_word_count($string,2));

    ?>

</body>

</html>

 

Output

str-word-count-php.gif
You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.