www.webdeveloper.com
Results 1 to 6 of 6

Thread: shorten url

  1. #1
    Join Date
    Nov 2003
    Posts
    287

    shorten url

    hi i want to shorten some urls that i take from my database i got this script but it doesn't work:

    PHP Code:
            <?
            $text 
    "http://www.uni-regensburg.de/Fakultaeten/philFakII/Psychologie/PsyII/beautycheck/english/index.htm";
            
    $chars 25;

            
    $niew $text." ";
            
    $niew substr($niew,0,$chars);
            
    $niew substr($niew,0,strrpos($niew,' '));
            
    $niew $niew."...";

            echo 
    $niew;
            
    ?>
    this outputs only the ...

    how to solve this??
    thanks in advance!

    Greetz Joost
    Zebdaag

  2. #2
    Join Date
    Oct 2006
    Posts
    1,297
    How about this?
    PHP Code:
    $url "http://www.uni-regensburg.de/Fakultaeten/philFakII/Psychologie/PsyII/beautycheck/english/index.htm";
    echo 
    preg_replace('\/(\w+\/)+''/.../'$url); 
    Last edited by so_is_this; 11-23-2006 at 02:51 PM. Reason: Remove JavaScript RegEx delimiters.

  3. #3
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by so_is_this
    PHP Code:
    echo preg_replace('/\/(\w+\/)+/''/.../'$url); 
    I think I'd write that a bit differently just in case there are any non-word characters in the string.
    PHP Code:
    echo preg_replace('|(?!</)/([^/]+/)+|''/.../'$url); 

  4. #4
    Join Date
    Oct 2006
    Posts
    1,297
    Yours replaces incorrectly (with those | in there)
    or replaces too much (without them).
    Without those | in yours, I get this:

    http://.../index.htm

    With mine, I get this:

    http://www.uni-regensburg.de/.../index.htm
    Last edited by so_is_this; 11-23-2006 at 02:49 PM.

  5. #5
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by so_is_this
    Yours replaces incorrectly
    It was a typo in the expression. I didn't test it. Should have been:
    Code:
    echo preg_replace('|(?<!/)/([^/]+/)+|', '/.../', $url);

  6. #6
    Join Date
    Oct 2006
    Posts
    1,297
    What are the vertical bars on both ends supposed to do in your suggestion? My understand is that those are supposed to be like "or" operators (alternative branch) -- meaning there should be a pattern on both sides of them. Perhaps you can teach me something by telling me how they are supposed to work in your suggestion. Because you're suggestion is still not working for me.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles