많은 포럼에서 길이를 줄이려면 중심에서 URL을 잘라 내고 3 개의 점을 추가하는 것으로 나타났습니다.PHP : URL을 줄이면 가운데에서 잘라낼 수 있습니까?
예 : ajaxify multipart encoded form (upload forms) --- --->
http://stackoverflow.c...ed-form-upload-forms 어떻게 할 것인가 순수 PHP를 사용하고 계십니까?
감사합니다.
많은 포럼에서 길이를 줄이려면 중심에서 URL을 잘라 내고 3 개의 점을 추가하는 것으로 나타났습니다.PHP : URL을 줄이면 가운데에서 잘라낼 수 있습니까?
예 : ajaxify multipart encoded form (upload forms) --- --->
http://stackoverflow.c...ed-form-upload-forms 어떻게 할 것인가 순수 PHP를 사용하고 계십니까?
감사합니다.
$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";
}
인쇄
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
을 통해 당신은 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>
@null 좋은 솔루션을 제공과 함께 substr 기능을 사용할 수 있습니다. 그러나 UTF-8 : http://de.wikipedia.org/wiki/Märchen을 인식하면><<가 중요한 바이트를 잃는 경우 잘못된 출력이 발생할 수 있습니다. 여기
는 mb_string functions을 사용하여 약간 개선 된 버전이다function short_url($url, $max_length=20)
{
mb_internal_encoding("UTF-8");
$real_length = mb_strlen($url);
if ($real_length <= $max_length)
{
return $url;
}
$keep = round($max_length/2) - 1;
return mb_substr($url, 0, $keep) . '…' . mb_substr($url, -$keep);
}
// Test
print short_url('http://de.wikipedia.org/wiki/Märchen', 13);
// http:/…ärchen - not nice, but still valid UTF-8. :)