Shorten Urls In Blog Using Preg_Replace

I have a problem with loooong urls submitted in my guestbook/blog.

I would like to replace the loong urls entered into the form with something like:


<a href="{real url}">{short url}</a> 

I would like the short url to be ended with “…” if it’s longer than X characters. I’ve tried to use preg_replace but that will simply cut the link based on the regular expression.


$urlPattern = "/(https?:\/\/)?(www.)?([-a-z0-9]*[a-z0-9]\.)(\bcom\b|\bbiz\b|\bgov\b|\bmil\b|\bnet\b|\borg\  b|[a-z][a-z]\.[a-z][a-z])\/?([a-zA-Z0-9]*)?([a-zA-Z0-9\.\?=\/&]*)?/";

$textNew = preg_replace($urlPattern,"<a href=\"$1$2$3$4/$5$6\" >$2$3$4/$5</a>", $textOriginal); 

This results in (example):




<a href="http://www.example1.com">www.example1.com</a>

<a href="http://www.example2.com/folder/function.php?variable=value&boolean=true">www.example2.com/folder</a> 

I would like it to result in:


<a href="http://www.example1.com">www.example1.com</a>

<a href="http://www.example2.com/folder/function.php?variable=value&boolean=true">www.example2.c....oolean=true</a> 

Anyone has an idea of how this could be done?

PS. I’m a noob when it comes to regular expressions

Hello,

You could use preg_replace_callback function - http://php.net/manual/en/function.preg-replace-callback.php. It allows you to use any formatting you want constructed from elements matched by pattern.

I’ve played with code below - it provides good result for your example. shortenUrl() is function from your post, shortenUrlNew() is new function.





function shortenUrl($textOriginal)

    {

        $urlPattern = "/(https?:\/\/)?(www.)?([-a-z0-9]*[a-z0-9]\.)(\bcom\b|\bbiz\b|\bgov\b|\bmil\b|\bnet\b|\borg\  b|[a-z][a-z]\.[a-z][a-z])\/?([a-zA-Z0-9]*)?([a-zA-Z0-9\.\?=\/&]*)?/";

        $textNew    = preg_replace($urlPattern, "<a href=\"$1$2$3$4/$5$6\" >$2$3$4/$5</a>", $textOriginal);


        return $textNew;

    }


    function shortenUrlNew($textOriginal)

    {

        $urlPattern = "/(https?:\/\/)?(www.)?([-a-z0-9]*[a-z0-9]\.)(\bcom\b|\bbiz\b|\bgov\b|\bmil\b|\bnet\b|\borg\  b|[a-z][a-z]\.[a-z][a-z])\/?([a-zA-Z0-9]*)?([a-zA-Z0-9\.\?=\/&]*)?/";

        $textNew    = preg_replace_callback($urlPattern, "replaceLinkParts", $textOriginal);


        return $textNew;

    }


    function replaceLinkParts($matches)

    {

        $howMuchSymbolsShouldBeLeft = 11;


        return


            '<a href="' . $matches[1]. $matches[2] . $matches[3] . $matches[4] . '/' . $matches[5] . $matches[6] . '">

            ' . $matches[2] . $matches[3] . substr($matches[4], 0, 1) . '....' .

            substr($matches[6], (strlen($matches[6]) - $howMuchSymbolsShouldBeLeft), $howMuchSymbolsShouldBeLeft).

            '</a>';

    }


    echo shortenUrl('http://www.example2.com/folder/function.php?variable=value&boolean=true');


    echo '

    <br><br>

    ';


    echo shortenUrlNew('http://www.example2.com/folder/function.php?variable=value&boolean=true');




If you have any questions, don’t hesitate to ask.

Hi EraQ

You can use URL Management both for seo and sorting url

http://www.yiiframework.com/doc/guide/1.1/en/topics.url#parameterizing-routes

Also check

http://www.yiiframework.com/wiki/404/hyphenation-of-routes-in-url-management/

Hi there,

If i use this for exp:


echo shortenUrlNew('http://www.yiiframework.com/forum/index.php/topic/38181-shorten-urls-in-blog-using-preg-replace/page__p__183810');



It will show just,


www.yiiframework.c....topic/38181-shorten-urls-in-blog-using-preg-replace/page__p__183810 

I need like this url:-)

http://www.yiiframework.com/forum/index.php/topic/38181-shorten-urls-in-blog-using-preg-replace/page__p__183810

http://www.yiiframework.com/forum/?tab=photo

http://www.englishanimes.com/watch/naruto-shippuden-episode-107-dub-online/

Try something like this:





function shortenUrlNew($textOriginal)

    {

        $urlPattern = "/(https?:\/\/)?(www.)?([-a-z0-9]*[a-z0-9]\.)(\bcom\b|\bbiz\b|\bgov\b|\bmil\b|\bnet\b|\borg\  b|[a-z][a-z]\.[a-z][a-z])\/?([a-zA-Z0-9]*)?([a-zA-Z0-9\.\?=\/&-_]*)?/";

        $textNew    = preg_replace_callback($urlPattern, "replaceLinkParts", $textOriginal);


        return $textNew;

    }


    function replaceLinkParts($matches)

    {

        $howMuchSymbolsShouldBeLeft = 15;


        return


            '<a href="' . $matches[1]. $matches[2] . $matches[3] . $matches[4] . '/' . $matches[5] . $matches[6] . '">

            ' . $matches[2] . $matches[3] . substr($matches[4], 0, 1) . '....' .

            substr($matches[6], (strlen($matches[6]) - $howMuchSymbolsShouldBeLeft), $howMuchSymbolsShouldBeLeft).

            '</a>';

    }


    echo shortenUrlNew('http://www.yiiframework.com/forum/index.php/topic/38181-shorten-urls-in-blog-using-preg-replace/page__p__183810');