Home > Backend Development > PHP Tutorial > How to use str_word_count function in php

How to use str_word_count function in php

藏色散人
Release: 2023-04-06 19:32:02
Original
2989 people have browsed it

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)
Copy after login

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!");
?>
Copy after login

Output:

3
Copy after login

php str_word_count() function usage example 2

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

Output:

6  
Copy after login

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!

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