PHP str_word_count

 The PHP str_word_count function can be used to return the word usage of the string String . Simply put, it can be used to determine the number of words in a string, or to split a word into multiple parts and store them as a PHP array . The two PHP string cutting functions str_split and mb_split , are a bit similar, except that str_word_count has more string word count statistics, and str_word_count cannot directly use regular notation to specify which part of the string to start cutting. PHP str_word_count basic syntax


str_word_count (string $string, int $format = 0, string $charlist)


The first parameter string $string is the string to be tested and is a required item. The second parameter int $format = 0 is used to specify the execution rules. The default is 0, which represents the number of words in the input string. , Can also be set to 1 or 2. When set to 1, str_word_count returns all words in the string and stored as a PHP array , when set to 2, the words in the returned string are stored as a PHP array , but the array The key number of is determined by the position of the word in the string. Here are some examples.

PHP str_word_count basic example
<?php
 $str = "How are you today michael?";
 
 // The number of words in the output string
 print_r(str_word_count($str,0));

 // Return the array and store each word in the array in order
 print_r (str_word_count($str, 1));
 
 // Return the array and store each word in the array
 successively , and the array key is arranged according to the character position where the word appears print_r(str_word_count($str, 2));
 
 // Directly output the number of words in the string
 echo str_word_count($str);
?>
The first output result of the above example is the same as the fourth output result, the number of words is 5, and the second and third output results are as follows:


Post a Comment

0 Comments