Count all words, including numbers in php string

To count words in a php string, we can usually use str_word_count, but I think that is not always a good solution

good example:

$var ="Hello world!"; echo str_word_count($str); print_r(str_word_count($str, 1)); 

-> Exit

  2 Array ( [0] => Hello [1] => world ) 

wrong example:

 $var ="The example number 2 is a bad example it will not count numbers and punctuations !!"; 

-> output:

  14 Array ( [0] => The [1] => example [2] => number [3] => is [4] => a [5] => bad [6] => example [7] => it [8] => will [9] => not [10] => count [11] => numbers [12] => and [13] => punctuations ) 

Is there a good predefined function to do this correctly or do I need to use preg_match ()?

+6
source share
8 answers

You can always split the line by spaces and count the results:

 $res = preg_split('/\s+/', $input); $count = count($res); 

With your line

 "The example number 2 is a bad example it will not count numbers and punctuations !!" 

This code will create 16 .

The advantage of using this parameter over explode(' ', $string) is that it will work on multi-line strings, as well as on tabs, and not just spaces. The disadvantage is that it is slower.

+4
source

The following use of count() and explode() will be an echo:

  The number 1 in this line will counted and it contains the following count 8

PHP:

 <?php $text = "The number 1 in this line will counted"; $count = count(explode(" ", $text)); echo "$text and it contains the following count $count"; ?> 

Edit:

Sidenote:
The regular expression can be changed to accept other characters that are not included in the standard set.

 <?php $text = "The numbers 1 3 spaces and punctuations will not be counted !! . . "; $text = trim(preg_replace('/[^A-Za-z0-9\-]/', ' ', $text)); $text = preg_replace('/\s+/', ' ', $text); // used for the function to echo the line of text $string = $text; function clean($string) { return preg_replace('/[^A-Za-z0-9\-]/', ' ', $string); } echo clean($string); echo "<br>"; echo "There are "; echo $count = count(explode(" ", $text)); echo " words in this line, this includes the number(s)."; echo "<br>"; echo "It will not count punctuations."; ?> 
+6
source

Use count(explode(' ', $var));

0
source

You can try this,

 <?php function word_count($sentence) { $break = explode(" ",$sentence); $count = count($break); return $count; } $count = "Count all words of this sentence"; echo word_count($count); //Output 6 ?> 

Read more on Word Count in PHP

0
source

You can also use the code below for me.

  function get_num_of_words($string) { $string = preg_replace('/\s+/', ' ', trim($string)); $words = explode(" ", $string); return count($words); } $string="php string word count in simple way"; echo $count=get_num_of_words($string); 

The result will be 7

0
source

I know the question is old, but I share the fix that I accepted for this.

 $str ="Hello world !"; // you can include allowed special characters as third param. print_r(str_word_count($str, 1, '!')); 

code output

 Array ( [0] => Hello [1] => world [2] => ! ) 

if you want to add more words that you can specify as the third parameter.

 print_r(str_word_count($str, 1, ' 0..9.~!@ #$%^&*()-_=+{}[]\|;:?/<>.,')); 

from 0..9. will include all numbness, and other special characters are inserted individually.

0
source

Just improve your decision.

 function stringWordNumberCount($text){ if (!$text) { return 0; } //Clean the text to remove special character $text = trim(preg_replace('/[^A-Za-z0-9\-]/', ' ', $text)); //Remove continus space on text $text = trim( preg_replace('/\s+/', ' ',$text)); //count space return count(explode(' ', $text)); } 
0
source

ans:

 function limit_text($text, $limit) { if(str_word_count($text, 0) > $limit) { $words = str_word_count($text, 2); $pos = array_keys($words); $text = substr($text, 0, $pos[$limit]) . '...'; } return $text; } 
-1
source

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


All Articles