search
HomeBackend DevelopmentPHP ProblemPHP string learning: How to know how many words are contained in a string

In the previous article "PHP String Learning: Converting the Size of the First Character (3 Methods)", we learned about converting the first character of each word in the string to uppercase Methods. This time we continue to look at the words in the PHP string and talk about the method of calculating the number of words in the string. Friends who are interested can learn~

How do we know how many words are contained in a string? ? How to count the number of words in a string? Let’s take a look at it today.

Method 1: With the help of array

we can split the string and convert each word in the string into an array element in the array; then Just count the number of array elements.

For example, I have a string like this. The number of words in the string is 9:

$str = "the quick brown fox jumps over the lazy dog";

You can first use the explode() function to split the string into several parts based on spaces. substrings, then combine these substrings into an array and return it.

$arr = explode(' ', $str);

Use var_dump($arr)Output to see the result:

PHP string learning: How to know how many words are contained in a string

It can be seen that an element in the array corresponds to the string A word; therefore we can use count() to count the number of array elements, and then we will get the number of words in the string. The implementation code is given below:

The output result is:

PHP string learning: How to know how many words are contained in a string

##2. Use str_word_count() function

In fact, PHP has a built-in function str_word_count() to count the number of words in a string.

The output result is:

PHP string learning: How to know how many words are contained in a string

It can be seen that directly using

str_word_count($str) in the above code example will Can return the number of words. Let’s take a look at the str_word_count() function:

str_word_count($string,$return,$char)The function accepts 1 required parameter $string, 2 optional Omitted parameters $return and $char.

Everyone knows what the required parameter $string means. The omitted parameter

$char is used to specify the special characters that are recognized as words, which will not be introduced here; let’s talk about them in detail. Omitted parameters$return.

Parameter

$return is used to specify the return value of the str_word_count() function. There are three values:

  • 0 - The default value, will return the number of words found (that is, the result above).

  • 1 - returns an array containing the words in the string.

The output result is:

PHP string learning: How to know how many words are contained in a string

  • 2 - Return An array where the keys are the word positions in the string and the keys are the actual words.

The output result is:

PHP string learning: How to know how many words are contained in a string

This is very helpful for us to manipulate the occurrence position of a certain word. For example, if you want to get the position where the specified word "brown" appears, you can use the array_search() function to search for a key value in the array and return the corresponding key name. (If the key value is found more than once in the array, the key name matching the first found key value is returned.)

The output result is:

PHP string learning: How to know how many words are contained in a string

Okay, that’s it for now. If you want to know anything else, you can click here. → →

php video tutorial

Finally, I recommend reading a classic course "

PHP String Processing (Jade Girl Heart Sutra Edition)", it's free~ come and learn !

The above is the detailed content of PHP string learning: How to know how many words are contained in a string. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version