HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

str_word_count

Description

The str_word_count of String for PHP return information about words used in a string.

Syntax

str_word_count(
    string $string,
    int $format = 0,
    ?string $characters = null
): array|int

Parameters

string

The string

format

Specify the return value of this function.

ValueDescription
0returns the number of words found
1returns an array containing all the words found inside the string
2returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself

characters

A list of additional characters which will be considered as 'word'.

Return

Returns an array or an integer, depending on the format chosen.

Examples

1 · string

<?

$string = "Hello fri3nd, you're
        looking        good today!";

$return = str_word_count($string);

echo $return;
7

2 · format · 0

<?

$string = "Hello fri3nd, you're
        looking        good today!";
$format = 0;

$return = str_word_count($string, $format);

echo $return;
7

3 · format · 1

<?

$string = "Hello fri3nd, you're
        looking        good today!";
$format = 1;

$return = str_word_count($string, $format);

print_r($return);
Array
(
    [0] => Hello
    [1] => fri
    [2] => nd
    [3] => you're
    [4] => looking
    [5] => good
    [6] => today
)

4 · format · 2

<?

$string = "Hello fri3nd, you're
        looking        good today!";
$format = 2;

$return = str_word_count($string, $format);

print_r($return);
Array
(
    [0] => Hello
    [6] => fri
    [10] => nd
    [14] => you're
    [23] => looking
    [38] => good
    [43] => today
)

5 · characters

<?

$string = "Hello fri3nd, you're
        looking        good today!";
$format = 2;
$characters = "!3";

$return = str_word_count($string, $format, $characters);

print_r($return);
Array
(
    [0] => Hello
    [6] => fri3nd
    [14] => you're
    [23] => looking
    [38] => good
    [43] => today!
)