[PHP] Use word_limiter() function to limit your words
June 1, 2010 12 Comments
There is nice little tricks to make meaningful summary text form big description less than a sec? ![]()
This example can also be used to create more link in short description.
This Function will cut words from Text and displaying limited words
function word_limiter($str,$limit=10)
{
if(stripos($str," ")){
$ex_str = explode(" ",$str);
if(count($ex_str)>$limit){
for($i=0;$i<$limit;$i++){
$str_s.=$ex_str[$i]." ";
}
return $str_s;
}else{
return $str;
}
}else{
return $str;
}
}
How does it work:
1. Define how many words you want to display.
2. Find what is the last character displaying.
3. If the last character displaying is not ” ” (Space) then again go next character until found.
4. Display words
<?php
/**
* @example one
*/
echo word_limiter('Hellya!!! this function really works nice for me.',4)."...";
//this Returns "Hellya!!! this function really ..."
?>



Excellent!!!
Thanks Bahar
Ya, this good but what happened if there is HTML tag in description.
oh ya!! good point. I will make it fit and update soon.
Thanks Sahbaj
Cool, Thanks to share
you welcome ru
Thanks!!! just what I need
you welcome!
Thanks You for help this information is very help ful for me
you welcome
what about that string which dose not contain the spaces?
Why do you want to limit a long length string with this method ??