php function Str_word_count () to calculate the number of words in a string

Source: Internet
Author: User

Instance

Count the number of words in the string "Hello world!":

<?phpecho str_word_count ("Hello world!");? >

Definition and usage

The Str_word_count () function calculates the number of words in a string.

Grammar

Str_word_count (String,return,char)
parameters description
string required. Specifies the string to check.
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 name is the position of the word in the string, and the key value is the actual word.

char optional. Specifies a special character that is identified as a word.

technical details

return value: Returns a number or an array, depending on the return parameter selected.
php version: 4.3.0+
update log: in PHP 5.1, a new char parameter was added.

More examples

Example 1

Returns an array containing the words in the string:

<?phpprint_r (Str_word_count ("Hello world!", 1));? >

Example 2

Returns an array where the key name is the position of the word in the string, and the key value is the actual word:

<?phpprint_r (Str_word_count ("Hello world!", 2));? >

Example 3

There are no char parameters and there are char parameters:

<?phpprint_r (Str_word_count ("Hello World & Good morning!", 1));p Rint_r ( Str_word_count ("Hello World & Good morning!", 1, "&")); 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.