Skip to content

Instantly share code, notes, and snippets.

@sstok
Created March 10, 2014 10:25
Show Gist options
  • Save sstok/9462609 to your computer and use it in GitHub Desktop.
Save sstok/9462609 to your computer and use it in GitHub Desktop.
<?php
function trimURL( $psInput )
{
if ( mb_strlen( $psInput ) < 39 )
{
return $psInput ;
}
$aURL = @ parse_url( $psInput );
if ( $aURL === false || ! isset( $aURL['scheme'] ) || isset( $aURL['user'], $aURL['pass'] ) )
{
return mb_substr( $psInput, 0, 25 ) . '...' . mb_substr( $psInput, -20 ) ;
}
$iHostLengt = mb_strlen( $aURL['host'] ) ;
if ( ( $aURL['scheme'] == 'http' || $aURL['scheme'] == 'https' || $aURL['scheme'] == 'ftp' || $aURL['scheme'] == 'ftps' ) && $iHostLengt < 20 )
{
$iHostLengt += mb_strlen( $aURL['scheme'] ) + 3;
$psInput = mb_substr( $psInput, $iHostLengt );
return $aURL['scheme'] . '://' . $aURL['host'] . mb_substr( $psInput, 0, 5 ) . '...' . mb_substr( $psInput, -15 ) ;
}
else
{
return mb_substr( $psInput, 0, 25 ) . '...' . mb_substr( $psInput, -20 ) ;
}
}
echo trimURL('http://rollerscapes.net/RF/Manual/Tuts/something.html')."\n";
echo trimURL('http://projects.rollerscapes.net/RF/Manual/Tuts/something.html')."\n";
echo trimURL('http://www.pfz.nl/scripts/400-encrypten-en-decrypten-met-persoonlijke-sleutel/')."\n";
echo trimURL('ftp://ftp.example.com/dir1/dir/dir3/dir3/dir3/dir3/dir3/file.html')."\n";
echo trimURL('ftp://ftp.foo.tree.bar.example.com/dir1/dir/dir3/dir3/dir3/dir3/dir3/file.html')."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment