Home  >  Article  >  Backend Development  >  How to use str_word_count function in php

How to use str_word_count function in php

藏色散人
藏色散人Original
2019-05-24 13:12:172949browse

Usage of str_word_count function in php: [str_word_count(string,return,char)]. The str_word_count function is used to count the number of words in a string, such as [print_r(str_word_count("I love",1)].

How to use str_word_count function in php

##php The str_word_count function is used to To count the number of words in a string, its syntax is str_word_count(string,return,char). The parameter string is required and specifies the string to be checked.

(Recommended tutorial:

php video tutorial)

php How to use str_word_count function?

Function: Count the number of words in a string.

Syntax:

str_word_count(string,return,char)

Parameters:

string Required. Specifies the string to be checked.

return Optional. Specifies the return value of the str_word_count() function.

Possible values:

0 - Default. Returns the number of words found.

1 - Returns an array containing the words in the string.

2 - Returns an array where the key is the position of the word in the string and the key is the actual word.

char Optional. Specifies the special character that is considered a word.

Instructions:

Calculate the number of words in the string.

php str_word_count() function usage example 1

<?php
echo str_word_count("I love php!");
?>

Output:

3

php str_word_count() function usage example 2

<?php
echo str_word_count("i study php at php.cn");
?>

Output:

6  

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

Statement:
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