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|intParameters
string
The string
format
Specify the return value of this function.
| Value | Description |
|---|---|
| 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 |
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!
)
Links
String
- 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
- printf
- quoted_printable_decode
- quoted_printable_encode
- quotemeta
- rtrim
- setlocale
- sha1
- sha1_file
- similar_text
- soundex
- sprintf
- sscanf
- str_contains
- str_decrement
- str_ends_with
- str_getcsv
- str_increment
- 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