How to use the str_word_count function in PHP to count the number of words in a string

WBOY
Release: 2023-06-26 13:24:02
Original
1587 people have browsed it

In PHP development, counting the number of words in a string is a common task. The str_word_count function in PHP provides a simple, fast and reliable way to count the number of words in a given string.

This article will introduce how to use the str_word_count function in PHP to count the number of words in a string, including the usage, return value and practical application scenarios of this function.

1. Function introduction

The str_word_count function is a string processing function built into PHP, which is used to count the number of words in a string. This function supports three parameters, which are the string to be counted, the pattern and the optional character set.

The function prototype is:

str_word_count(string $string, int $format = 0, string|null $char_list = null): mixed

Among them, $string means to Statistical string, $format represents the format of the returned result, $char_list is an optional character set parameter.

2. Return value

The return value of the str_word_count function depends on the setting of the $format parameter. Specifically, the $format parameter has the following options:

  1. $format = 0: Returns the number of all words in the string, the default value is 0.
  2. $format = 1: Returns an array containing the starting position of each word in the string.
  3. $format = 2: Returns an array containing each word.

3. Usage Examples

The following uses several examples to illustrate the use of the str_word_count function.

  1. Count the number of words in the string

$string = "Hello world! How are you?";
$count = str_word_count($string);
echo "The number of words is: ".$count;
?>

The output result is:

The number of words is: 6

  1. Return an array of words

$string = "Hello world! How are you?";
$words = str_word_count($string , 2);
print_r($words);
?>

The output result is:

Array
(

[0] => Hello
[1] => world
[2] => How
[3] => are
[4] => you
Copy after login

)

  1. Return the position of each word

$string = "Hello world! How are you?";
$positions = str_word_count( $string, 1);
print_r($positions);
?>

The output result is:

Array
(

[0] => 0
[1] => 6
[2] => 12
[3] => 16
[4] => 20
Copy after login

)

4. Summary

This article introduces the basic usage of the str_word_count function in PHP and the meaning of the return value. Through this function, we can easily count the number of words in the string and their positions, so as to better perform string processing.

In addition to counting the number of words, the str_word_count function can also be used to implement other functions, such as deleting all words or converting words into arrays. In actual development, we can use this function flexibly to make the code more concise and efficient.

The above is the detailed content of How to use the str_word_count function in PHP to count the number of words in a string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!