str_word_count

Return information about words used in a string

Syntax

str_word_count ( string $string [, int $format = 0 [, string $charlist ]] ) : mixed

Parameters

string

The string

format

Specify the return value of this function.

0 returns the number of words found
1 returns an array containing all the words found inside 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
charlist

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 · charlist

<?

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

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

print_r($return);

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

addcslashes

addslashes

bin2hex

chop

chr

chunk_split

convert_uudecode

convert_uuencode

count_chars

crc32

crypt

echo

explode

fprintf

get_html_translation_table

hebrev

hebrevc

hex2bin

html_entity_decode

htmlentities

htmlspecialchars

htmlspecialchars_decode

implode

join

lcfirst

levenshtein

localeconv

ltrim

md5

md5_file

metaphone

nl_langinfo

nl2br

number_format

ord

parse_str

print

printf

quoted_printable_decode

quoted_printable_encode

quotemeta

rtrim

setlocale

sha1

sha1_file

similar_text

soundex

sprintf

sscanf

str_contains

str_ends_with

str_getcsv

str_ireplace

str_pad

str_repeat

str_replace

str_rot13

str_shuffle

str_split

str_starts_with

strcasecmp

strchr

strcmp

strcoll

strcspn

strip_tags

stripcslashes

stripos

stripslashes

stristr

strlen

strnatcasecmp

strnatcmp

strncasecmp

strncmp

strpbrk

strpos

strrchr

strrev

strripos

strrpos

strspn

strstr

strtok

strtolower

strtoupper

strtr

substr

substr_compare

substr_count

substr_replace

trim

ucfirst

ucwords

vfprintf

vprintf

vsprintf

wordwrap

PHP

Home Menu