0

I have a search result like

1. my title
my short description......
http://www.stackoverflow.com/tags/thisthat/againthisthat/againandagainthisthat/mypage.html
http://www.stackoverflow.com/tags/mypage.html?a=123123123&b=2342343

I want the url in this format

1. my title
my short description......
http://www.stackoverflow.com/tags/......./againandagainthisthat/mypage.html
http://www.stackoverflow.com/tags/mypage.html?a....3123&b=2342343

Some text are skipped in middle of the link

I tried to google it but dont know the exact keyword to search..

What ever my link is, if the length of that link is over 70 char ,lets say it has 100 then link is minimized to 70 char with ..... at the middle....

sujal
  • 1,058
  • 3
  • 18
  • 29
  • 1
    possible duplicate of [How to mimic StackOverflow Auto-Link Behavior](http://stackoverflow.com/questions/1925455/how-to-mimic-stackoverflow-auto-link-behavior) – Alix Axel Apr 04 '12 at 09:11
  • I neet in middle not at the end – sujal Apr 04 '12 at 09:25

2 Answers2

1

This works (for original example):

$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;

Basic if more than 70 take first 32 chars, last 32 chars and '......' in the middle:

$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;
Ingmar Boddington
  • 3,440
  • 19
  • 38
  • Does not works for http://pt.php.net/function.base-convert.php?a=232323232323&b=234234234 this link – sujal Apr 04 '12 at 09:23
  • I think you need to be more specific of what you are after. Clearly define your conditions which will trigger shortening and then how you would like them to be shortened.... – Ingmar Boddington Apr 04 '12 at 09:33
  • 1
    what ever my link is, if the length of that link is over 70 char ,lets say it has 100 then link is minimized to 70 char with ..... at the middle.... – sujal Apr 04 '12 at 09:39
  • k, next edit has an example to do that (not some of the example urls you have given are in fact less than 70 chars) – Ingmar Boddington Apr 04 '12 at 09:46
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;
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
  • Thanks but doesnot works for http://www.stackoverflow.com/tags/mypage.html?a=123123123&b=2342343 this link – sujal Apr 04 '12 at 09:29