2012-04-04 69 views
0

我有一個像如何最大限度地減少在搜索結果中的URL鏈接

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字符與.....在中間....

+1

的可能重複[如何模仿StackOverflow的自動鏈接行爲(http://stackoverflow.com/questions/1925455/how-to-mimic-stackoverflow-auto-link-behavior) – 2012-04-04 09:11:22

+0

我NEET在中間不在最後 – sujal 2012-04-04 09:25:36

回答

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; 
+0

不適用http://pt.php.net/function.base-convert.php?a=232323232323&b=234234234此鏈接 – sujal 2012-04-04 09:23:41

+0

我認爲你需要更具體的你是什麼後。清楚地定義你的條件,這將引發縮短,然後你怎麼想他們被縮短... – Ing 2012-04-04 09:33:57

+1

什麼是我的鏈接,如果該鏈接的長度超過70個字符,可以說它有100個,然後鏈接被最小化爲70字符.....在中間.... – sujal 2012-04-04 09:39:07

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; 
+0

謝謝,但不適用於http://www.stackoverflow.com/tags/mypage.html?a=123123123&b=2342343此鏈接 – sujal 2012-04-04 09:29:55

相關問題