Posted By

svenito on 09/29/09


Tagged

array String length shorten


Versions (?)

Shorten strings in an array to a maximum length


 / Published in: PHP
 

Shortens all strings in an array to a maximum length. Inserts '...' in between the first and last 6 letters

  1. public static function shortenNames($names) {
  2. $short_names = array();
  3. foreach ($names as $name) {
  4. if (strlen($name) > 15) {
  5. $front = substr($name, 0, 6);
  6. $back = substr($name, -6, 6);
  7. array_push($short_names, $front.'...'.$back);
  8. } else {
  9. array_push($short_names, $name);
  10. }
  11. }
  12. return $short_names;
  13. }

Report this snippet  

You need to login to post a comment.