How to count words in a specific string in PHP?

I want to count words in a specific line, so I can check it and prevent users from writing more than 100 words, for example.

I wrote this function, but I don’t think it is efficient enough, I used the gap function with space as a separator, but what if the user puts two spaces instead of one. Can you give me a better way to do this?

function isValidLength($text , $length){ $text = explode(" " , $text ); if(count($text) > $length) return false; else return true; } 
+7
source share
9 answers

Maybe str_word_count can help

http://php.net/manual/en/function.str-word-count.php

 $Tag = 'My Name is Gaurav'; $word = str_word_count($Tags); echo $word; 
+17
source

You can use the built-in PHP function str_word_count . Use it like this:

 $str = "This is my simple string."; echo str_word_count($str); 

This will lead to conclusion 5.

If you plan to use special characters in any of your words, you can specify any additional characters as the third parameter.

 $str = "This weather is like el ninã."; echo str_word_count($str, 0, 'àáã'); 

This will print 6.

+10
source

Try the following:

 function get_num_of_words($string) { $string = preg_replace('/\s+/', ' ', trim($string)); $words = explode(" ", $string); return count($words); } $str = "Lorem ipsum dolor sit amet"; echo get_num_of_words($str); 

This will output: 5

+6
source

This function uses a simple regular expression to split the input text $ into any non-letter character:

 function isValidLength($text, $length) { $words = preg_split('#\PL+#u', $text, -1, PREG_SPLIT_NO_EMPTY); return count($words) <= $length; } 

This ensures correct operation with words separated by several spaces or any other non-letter character. It also correctly processes unicode (e.g. accented letters).

The function returns true when the number of words is less than $ length.

+4
source

str_count_words has its drawbacks. it will read underscores as separated words like this_is two words:

You can use the following function to count words separated by spaces, even if more than one exists between them.

 function count_words($str){ while (substr_count($str, " ")>0){ $str = str_replace(" ", " ", $str); } return substr_count($str, " ")+1; } $str = "This is a sample_test"; echo $str; echo count_words($str); //This will return 4 words; 
+4
source

Use preg_split () instead of explode (). Split supports regular expressions.

+2
source

Using substr_count to count the number of occurrences of any substring. to find the number of words given by $ needle equal to. '' int substr_count (string $ haystack, string $ needle)

 $text = 'This is a test'; echo substr_count($text, 'is'); // 2 echo substr_count($text, ' ');// return number of occurance of words 
+1
source

There are n-1 spaces between n objects, so there will be 99 spaces between 100 words, so you can choose the average word length, for example, 10 characters, then multiply by 100 (for 100 words), then add 99 (spaces), you can instead, limit the number of characters (1099).

 function isValidLength($text){ 

if (strlen ($ text)> 1099)

  return false; 

else return true;

}

0
source

I wrote a function that is better than str_word_count , because this PHP function considers dashes and other characters as words.

Also my function solves the problem of double spaces, which many of the functions written by other people are not taken into account.

This function also processes HTML tags. If you had two tags nested together and just used the strip_tags function, that would be considered one word when it is two. For example: <h1>Title</h1>Text or <h1>Title</h1><p>Text</p>

In addition, I first exclude JavaScript, but the code in the <script> tags will be counted as words.

Finally, my function handles spaces at the beginning and end of a line, several spaces and line breaks, and returns tab and tab characters.

 ############### # Count Words # ############### function count_words($str) { $str = preg_replace("/[^A-Za-z0-9 ]/","",strip_tags(str_replace('<',' <',str_replace('>','> ',str_replace(array("\n","\r","\t"),' ',preg_replace('~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is','',$str)))))); while(substr_count($str,' ')>0) { $str = str_replace(' ',' ',$str); } return substr_count(trim($str,' '),' ')+1; } 
0
source

All Articles