Web Hosting Talk







View Full Version : Short URL - php function


Fahad_1
11-26-2006, 02:32 AM
Hello,

I need a php function so that a long url becomes short (for displaying only, the link will be original)

For e.g, a URL like this : mysite.com/dir/site/go/index.php?id=main&go=1 to be turned into this : mysite.com/dir...&go=1

I want this script for the display only so that it doesnt take too much space on the screen, but the hyperlink will be the oroginal.

Can any1 help me with this pls?

Thanks.

Ogg
11-26-2006, 03:07 AM
Look up mod_rewrite. That's what you're looking for.

Burhan
11-26-2006, 03:08 AM
You can do this with the built in substr_replace() (http://php.net/substr_replace) function combined with parse_url() (http://php.net/parse_url[/url).

If you need more help, just ask :)

Fahad_1
11-26-2006, 03:35 AM
Actually, the url will be dynamic , and i have no idea what the url will be.

Is it gonna be something like this:

<?
$url = "mysite.com/dir/go/and/page.php?id=asd&me=ok&how=this";
$length = strlen($url);
if($lenth > 20) {
$first = substr($url, 0, 10);
$last = substr($url, $length - 10, $length);
$output = $first . "..." . $last;
} else {
$output = $url;
}
?>


Please reply.

localhost127
11-26-2006, 03:43 AM
The code that you posted looks fine, what is the problem?

Saeven
11-26-2006, 06:03 PM
Careful of the little typo in your code:


<?
$url = "mysite.com/dir/go/and/page.php?id=asd&me=ok&how=this";
$length = strlen($url);
if($length > 20) {
$first = substr($url, 0, 10);
$last = substr($url, $length - 10, $length);
$output = $first . "..." . $last;
} else {
$output = $url;
}
?>


Although verbose, should work well :)

ic3d
11-28-2006, 04:08 PM
You can also use POST sessions to keep the urls shorter.

Saeven
11-28-2006, 04:14 PM
You can also use POST sessions to keep the urls shorter.I think there's a misunderstanding, given that this doesn't fall into context with this thread, which deals with representation length of URLs. In the end, one cannot reduce a URL to use POST - we'd have to replace the URL with a form, which falls outside the scope of this thread..