i've seen in many forums that they cut the url from the center and add 3 dots if it's long in order to shorten it.
Example: ajaxify multipart encoded form (upload forms) ---Will be---> http://stackoverflow.c...ed-form-upload-forms
How to do that using pure php?
Thank you
e.g. via preg_replace()
$testdata = array(
'http://stackoverflow.com/questions/1899537/ab',
'http://stackoverflow.com/questions/1899537/abc',
'http://stackoverflow.com/questions/1899537/abcd',
'http://stackoverflow.com/questions/1899537/ajaxify-multipart-encoded-form-upload-forms'
);
foreach ($testdata as $in ) {
$out = preg_replace('/(?<=^.{22}).{4,}(?=.{20}$)/', '...', $in);
echo $out, "\n";
}
prints
http://stackoverflow.com/questions/1899537/ab
http://stackoverflow.c...uestions/1899537/abc
http://stackoverflow.c...estions/1899537/abcd
http://stackoverflow.c...ed-form-upload-forms
You can use the substr function along with strlen
$url = "http://stackoverflow.com/questions/1899537/";
if(strlen($url) > 20)
{
$cut_url = substr($url, 0, 6);
$cut_url .= "...";
$cut_url .= substr($url, -6);
}
<a href="<?=$url; ?>"><?=$cut_url;?></a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With