Shorten strings in an array to a maximum length


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

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


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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.