shorten a string


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

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


Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.