Jump to: navigation, search

Php str word count

From w3cyberlearnings

Contents

PHP function str_word_count

This function counts the number of words in a string.

Syntax str_word_count

  • string1: String input
  • returntype: 0 (Default- Number of word), 1( Array), 2 ( Key=>Value, key is the position of the word, and value is the actual word)
  • char: Specifies the char as word.
str_word_count(string1, string2)

Return

  • 0: when two string equal.
  • >0: string1 is greater than string2.
  • <0: string2 is greater than string1.

Example 1

<?php

$str="The world is full of leader.";
echo str_word_count($str);
?>

Output

6

Example 2

<?php

$str = "The world is full of leader.";
$aa = str_word_count($str, 1);
print_r($aa);
?>

Output

Array
(
    [0] => The
    [1] => world
    [2] => is
    [3] => full
    [4] => of
    [5] => leader
)

Example 3

<?php

$str = "The world is full of leader.";
$aa = str_word_count($str, 2);
print_r($aa);
?>

Output

Array
(
    [0] => The
    [4] => world
    [10] => is
    [13] => full
    [18] => of
    [21] => leader
)

Example 4

<?php

$str="Jany & Bo; God & Human";
$aa1 =  str_word_count($str,1);
$aa2 =  str_word_count($str,1,'&');

print_r($aa2);
?>

Output

Array
(
    [0] => Jany
    [1] => &
    [2] => Bo
    [3] => God
    [4] => &
    [5] => Human
)

Related Functions


Navigation
Web
SQL
MISC
References