Strip Text of given length


/ Published in: PHP
Save to your folder(s)

Gets string and length and return string of given length. Makes sure half word in not in the end.


Copy this code and paste it in your HTML
  1. /*
  2. * stripText()
  3. * @action:get input string and returns splited string of given length
  4. * makes sure half word in not in the end
  5. * @parameters:
  6. * $str: string to be splited
  7. * $len: length of required string
  8. * @return: string of given length
  9. * @modified : 19 September 2010
  10. * @modified by: Nilambar
  11. */
  12. function stripText($str,$len=100)
  13. {
  14. $str=trim($str);
  15. $str=strip_tags($str);
  16. $slen=strlen($str);
  17. $op='';
  18. if($slen<=$len)
  19. {
  20. return $str;
  21. }
  22. else
  23. {
  24. $str=substr($str,0,$len);
  25. $exploded_array=explode(' ',$str );
  26. $wordcount=count($exploded_array);
  27.  
  28. $last=array_pop($exploded_array);
  29. $op=implode(' ',$exploded_array);
  30. $op.='...';
  31. }
  32. return $op;
  33.  
  34. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.