Skip to content

Instantly share code, notes, and snippets.

@matthewpizza
Created March 16, 2012 18:52
Show Gist options
  • Save matthewpizza/2051821 to your computer and use it in GitHub Desktop.
Save matthewpizza/2051821 to your computer and use it in GitHub Desktop.
Truncate Text
<?php
// From: http://css-tricks.com/snippets/php/truncate-string-in-center/
$longString = 'abcdefghijklmnopqrstuvwxyz0123456789z.jpg';
$separator = '/.../';
$separatorlength = strlen($separator) ;
$maxlength = 25 - $separatorlength;
$start = $maxlength / 2 ;
$trunc = strlen($longString) - $maxlength;
echo substr_replace($longString, $separator, $start, $trunc);
//prints "abcdefghij/.../56789z.jpg"
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment