Dec
27
2010

Shorten Text or link / URL in PHP – script

Sometimes, when you want to display the URL on a web page, it can be so long it looks untidy and messes up your layout. Of course, you can come up with suitable text for a hyperlink instead of showing the URL, but what about when a user posts a web address to your web site? Here’s a little script that, can be integrated directly into your site.

function shorten_link($link)
{
preg_match("#<a(.*?)>(.*?)</a>#",$link,$m);
for($i = 1;$i<sizeof($m)-1;$i++)
$t .= $m[$i];
return"<a{$t}>".str_replace("http://{$_SERVER['SERVER_NAME']}"."{$_SERVER['REQUEST_URI']}",
substr($_SERVER['REQUEST_URI'],1),end($m))."</a>";
}

How does it work:
Firstly it performs a regular expression, that gets the parameters of the <a> tag and then
innerHTML of the <a> tag (what’s between <a> and </a>).
This way when you are using a CMS, and it has a lot of parameters for <a> like
<a class=”some_class” id=”some_id” alt=”some_alt” href=”some_href”> it saves and in the end you get the whole link with the innerHTML shortend.
Usage:
$link is the whole link to some page in your site. For instance, if you have a link like <a href=”myawesomesite.com/awesomepage/awesomepage2/thepage.html”>
myawesomesite.com/awesomepage/awesomepage2/thepage.html</a>
then it would be shortened to <ahref=”myawesomesite.com/awesomepage/awesomepage2/thepage.html”> awesomepage/awesomepage2/thepage.html</a>

Enter Your Mail Address

Incoming search terms:

Related And Similar Posts

Leave a comment

*

Facebook Fans

Share Me With Google Plus!

Popular Posts

Social Bookmarking sites list 2012 - New and fresh bookmarking sites
Comments93
Bulgarian proxy or how to open arenabg and zamunda
Comments65
+ 150 New Free Directory Sites List 2012 - Sorted by Page Rank
Comments22
Scrapebox crack free download ?
Comments19
Great List With Google Plus / FB likes/ tweets exchange sites - Hot Topic
Comments16
How to make World Of Warcraft Cataclysm Server
Comments10
The Best Bulgarian Torrents - Listing
Comments9
500 Free Directory List 2011 !
Comments9
Free RSS Directory List Where To Submit Your Site
Comments9
Guest Blogs List and Useful Information
Comments8

Recent Posts

Calendar

December 2010
M T W T F S S
    Jan »
 12345
6789101112
13141516171819
20212223242526
2728293031