Guest User

Untitled

a guest
Jun 17th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. <?php
  2. /*
  3. Truncate Long String Exactly In Middle
  4.  
  5. This will truncate a longer string to a smaller string of specified length (e.g. the “25″ value
  6. in the code below) while replacing the middle portion with a separator exactly in the middle.
  7. Useful when you need to truncate a string but still show the beginning (e.g. for sorting and
  8. because it is most recognizable) and also show the end (perhaps to show a file name).
  9. */
  10.  
  11. $longString = 'abcdefghijklmnopqrstuvwxyz0123456789z.jpg';
  12. $separator = '/.../';
  13. $separatorlength = strlen($separator) ;
  14. $maxlength = 25 - $separatorlength;
  15. $start = $maxlength / 2 ;
  16. $trunc = strlen($longString) - $maxlength;
  17.  
  18. echo substr_replace($longString, $separator, $start, $trunc);
  19.  
  20. //prints "abcdefghij/.../56789z.jpg"
  21.  
  22. ?>
Add Comment
Please, Sign In to add comment