How can I get the number of spaces in a paragraph in PHP?

Say I had the following:

$name= "albert slevir inshten";

For the above, I would get 3.

+3
source share
5 answers

PHP provides a function called substr_countthat counts the number of occurrences of a substring:

$count = substr_count($str, ' '); //2

To get the word count, you should use str_word_count:

$count = str_word_count($str); //3
+6
source

to count substring entries, use: substr_count()

to count the number of words, use: str_word_count()

+3
source
0
source

I think there are only 2 spaces in this example ...

Offering the following:

$no = count(explode(" ",$str))-1;

You can leave -1 if you want to know the number of words (for example, 3 in your example)

0
source

PHP has a function called substr_count(), you can use it like this:

$name= "albert slevir inshten";

echo substr_count($name, ' ');
0
source

Source: https://habr.com/ru/post/1786495/


All Articles