<?php
mix str_word_count ( str $string [,
int $format = 0 [,
str $charlist ]] )
where,
$string = The STRING for word count
$format = To control how the STRING will be examined
( SEE the below TABLE )
$charlist = A list of additional characters
which will be considered as word
?>
VALUE | WHAT RETURN |
0 | the number of words found |
1 | an array containing all the words found inside the $string |
2 | an associative array, where the key is the numeric position of the word inside the $string and the value is the actual word itself |
ed48 |
<?php
$txt01 = "The quick brown fox jumps over the lazy dog.";
$cnt01 = str_word_count($txt01);
echo $txt01 . '<br>[ ' . $cnt01 . ' words ]<br>';
?>
<?php
$txt02 = "The quick brown fox jumps over the lazy dog.";
$cnt01 = str_word_count($txt02);
$cnt02 = str_word_count($txt02, 1);
echo $txt02 . '<br>[ ' . $cnt01 . ' words ]<br><br>';
foreach($cnt02 as $cn02 => $ct02)
{
echo $cn02 . ' - ' . $ct02 . '<br>';
}
?>
<?php
$txt03 = "The quick brown fox jumps over the lazy dog.";
$cnt01 = str_word_count($txt03);
$cnt03 = str_word_count($txt03, 2);
echo $txt03 . '<br>[ ' . $cnt01 . ' words ]<br><br>';
foreach($cnt03 as $cn03 => $ct03)
{
echo 'At position ' . $cn03 . ' we have: "' . $ct03 . '"<br>';
}
?>
<?php
$a = "";
var_dump(str_word_count($a));
try {
var_dump(str_word_count($a, -1));
} catch (ValueError $e) {
echo $e->getMessage() . "<br><br>";
}
try {
var_dump(str_word_count($a, 3, $a));
} catch (ValueError $e) {
echo $e->getMessage() . "<br><br>";
}
var_dump($a);
?>
<?php
$str = "Hello friend, you're
looking good today!";
$b =& $str;
var_dump(str_word_count($str, 1));
echo '<br><br>';
var_dump(str_word_count($str, 2));
echo '<br><br>';
var_dump(str_word_count($str));
echo '<br><br>';
try {
var_dump(str_word_count($str, 3));
} catch (\ValueError $e) {
echo $e->getMessage() . "<br><br>";
}
try {
var_dump(str_word_count($str, 123));
} catch (\ValueError $e) {
echo $e->getMessage() . "<br><br>";
}
try {
var_dump(str_word_count($str, -1));
} catch (\ValueError $e) {
echo $e->getMessage() . "<br><br>";
}
try {
var_dump(str_word_count($str, 999999999));
} catch (\ValueError $e) {
echo $e->getMessage() . "<br><br>";
}
var_dump($str);
$str2 = "F0o B4r 1s bar foo";
var_dump(str_word_count($str2, NULL, "04"));
echo '<br><br>';
var_dump(str_word_count($str2, NULL, "01"));
echo '<br><br>';
var_dump(str_word_count($str2, NULL, "014"));
echo '<br><br>';
var_dump(str_word_count($str2, NULL, ""));
echo '<br><br>';
var_dump(str_word_count($str2, 1, "04"));
echo '<br><br>';
var_dump(str_word_count($str2, 1, "01"));
echo '<br><br>';
var_dump(str_word_count($str2, 1, "014"));
echo '<br><br>';
var_dump(str_word_count($str2, 1, ""));
echo '<br><br>';
var_dump(str_word_count($str2, 2, "04"));
echo '<br><br>';
var_dump(str_word_count($str2, 2, "01"));
echo '<br><br>';
var_dump(str_word_count($str2, 2, "014"));
echo '<br><br>';
var_dump(str_word_count($str2, 2, ""));
echo '<br><br>';
var_dump(str_word_count("foo'0 bar-0var", 2, "0"));
echo '<br><br>';
var_dump(str_word_count("'foo'", 2));
echo '<br><br>';
var_dump(str_word_count("'foo'", 2, "'"));
echo '<br><br>';
var_dump(str_word_count("-foo-", 2));
echo '<br><br>';
var_dump(str_word_count("-foo-", 2, "-"));
?>