Posted By

sveggiani on 01/29/09


Tagged

strings not-mine


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


shorten a string


 / Published in: PHP
 

URL: http://www.milesj.me/blog/read/15/5-custom-basic-php-string-functions

Took from http://www.milesj.me/blog/read/15/5-custom-basic-php-string-functions

  1. /**
  2.  * If a string is too long, shorten it in the middle
  3.  * @param string $text
  4.  * @param int $limit
  5.  * @return string
  6.  */
  7. function shorten($text, $limit = 25) {
  8. if (strlen($text) > $limit) {
  9. $pre = substr($text, 0, ($limit / 2));
  10. $suf = substr($text, -($limit / 2));
  11. $text = $pre .' ... '. $suf;
  12. }
  13.  
  14. return $text;
  15. }

Report this snippet  

You need to login to post a comment.