Word Limit of a string In PHP



Sometimes, if we need limit in the text, but if you are using the inbuilt functions of php then it breaks the words, and count only characters not the word. In this post you will see the very useful PHP function which can be used in limit of word. It counts word not counts the characters.
<?php
 function countwordscustom($string, $limit) {
 $str_code = explode(" ",$string);
 return implode("",array_splice($str_code,0,$limit));
 }
 ?>

If you want to use this function anywhere in your PHP file follow given example

<?php
 $text = "EWA is a very good website the design of the website is very cool and good looking.";
 echo countwords($text,7);
 ?>

It will display only 7 words “EWA is a very good website the” and your word will never break.

Thanks and Enjoy the Coding.