Skip to content

Instantly share code, notes, and snippets.

@janrenn
Created November 27, 2017 14:37
Show Gist options
  • Save janrenn/7455d16644f07a05cef14b9a8bd560a3 to your computer and use it in GitHub Desktop.
Save janrenn/7455d16644f07a05cef14b9a8bd560a3 to your computer and use it in GitHub Desktop.
//truncation
//-------------
$ext = end( explode( '.', $filename ) );
$filename = substr( $filename, 0, -(strlen($ext) + 1) );
$filename = str_replace( '.', '-', $filename );
$filename_shortened = substr( $filename, 0, 70 );
if ( $filename_shortened != $filename ) {
$filename = preg_replace( '~(-|_)[^\-_]{0,' . floor( strlen($filename) / 2 ) . '}$~', '', $filename_shortened );
}
$filename = trim( $filename, '-_' );
$filename = $filename . '.' . $ext;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment