0

我有一个类似的搜索结果

1. 我的标题
我的简短描述......
http://www.stackoverflow.com/tags/thisthat/againthisthat/againandagainthisthat/mypage.html
http://www.stackoverflow.com/tags/mypage.html? a=123123123&b=2342343

我想要这种格式的网址

1. 我的标题
我的简短描述......
http://www.stackoverflow.com/tags/......./againandagainthisthat/mypage.html
http://www.stackoverflow.com/tags/ mypage.html?a....3123&b=2342343

链接中间跳过了一些文本

我试图用谷歌搜索它,但不知道要搜索的确切关键字..

我的链接是什么,如果该链接的长度超过 70 个字符,假设它有 100 个,则链接最小化为 70 个字符,中间......

4

2 回答 2

1

这有效(对于原始示例):

$url = 'http://www.stackoverflow.com/tags/thisthat/againthisthat/againandagainthisthat/mypage.html';
$urlBitsArray = explode('/', $url);
$urlBitsCount = count($urlBitsArray);
$newUrl = implode('/', array($urlBitsArray['0'], $urlBitsArray['1'], $urlBitsArray['2'], $urlBitsArray['3'], '.....', $urlBitsArray[$urlBitsCount - 2], $urlBitsArray[$urlBitsCount - 1]));
echo $newUrl;

基本,如果超过 70 个,前 32 个字符,后 32 个字符和 '......' 在中间:

$url = 'http://www.stackoverflow.com/tags/thisthat/againthisthat/againandagainthisthat/mypage.html ';

if (strlen($url) > 70) {
    $url = substr($url, 0, 31).'......'.substr($url, strlen($url) - 33);
}

echo $url;
于 2012-04-04T09:16:41.880 回答
0
<?php
$string = "http://www.stackoverflow.com/tags/thisthat/againthisthat/againandagainthisthat/mypage.html";
$maxStringLength = 50;

if(strlen($string) > $maxStringLength)
{
    //remove http://
    if(strpos($string, "http://") === 0)
    {
        $string = substr($string, 7);
    }
    $bits = explode("/", $string);
    if(count($bits) > 2) //greater than www.stackoverflow.com/mypage.html
    {
        $string = implode("/", array($bits[0], $bits[1], '...', $bits[count($bits)-2], $bits[count($bits)-1]));
    }
}

echo $string;
于 2012-04-04T09:21:01.837 回答