Results 1 to 8 of 8
  1. #1

    Short URL - php function

    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.
    Fahad
    http://frinity.blogspot.com - a blog from a web addict

  2. #2
    Join Date
    Aug 2002
    Location
    Canada!
    Posts
    159
    Look up mod_rewrite. That's what you're looking for.

  3. #3
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    You can do this with the built in substr_replace() function combined with parse_url().

    If you need more help, just ask
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  4. #4
    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.
    Fahad
    http://frinity.blogspot.com - a blog from a web addict

  5. #5
    Join Date
    Sep 2005
    Location
    Southern California
    Posts
    179
    The code that you posted looks fine, what is the problem?

  6. #6
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    665
    Careful of the little typo in your code:

    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
    circlical - hosting software development
    forums * blog

  7. #7
    Join Date
    Aug 2002
    Posts
    512
    You can also use POST sessions to keep the urls shorter.

  8. #8
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    665
    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..
    circlical - hosting software development
    forums * blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •