Get the App
SLTechnology News&Howtos  ›  Development  › 

How php counts the number of words in a string

Shulou Source: shulou.com Published: 2022-06-03 17:25:00 11月13日 Update

This article is about how php counts the number of words in a string. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. With the help of the array, divide the string, convert each word in the string into the array elements in the array, and count the number of array elements.

For example, I have a string with 9 words in the string:

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

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

$arr = explode ('', $str)

2. Using the str_word_count () function, PHP directly calculates the number of words in the string on the built-in function str_word_count ().

Thank you for reading! This is the end of the article on "how to count the number of words in a string by php". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Characters strings words arrays numbers statistics functions elements content more articles good practical size articles see knowledge spaces references help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Huawei macOS Redmi MySQL