SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Finding URLs

  1. #1
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I am using this code to find and replace URLs in a form submitted by users :

    PHP Code:
    $Comment preg_replace("/((?<!<a href=\")(http|https|ftp):\/\/[\w?=&.\/-]+)/","<a href=\"\\1\" target=\"_blank\">\\1</a>",$Comment); 
    How do I replace URLs when a user types it without http:// or www. ?

    Thanks for any help

    Sean

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Who says it's a url if it doesn't have http:// or www? How do you know it's not their name or address? I would do error checking on the form field where they are supposed to enter a url. But if that isn't feasible, you don't have any options. A url distinguishes itself from other text by the http:// or www.

  3. #3
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for the reply.

    What I mean is how do I replace the URL if someone enters www.sitepoint.com instead of http://www.sitepoint.com, or sitepoint.com instead of http://www.sitepoint.com ?

    Sean

  4. #4
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From the last user comment in PHP's ereg_replace:


    Played a bit with the ereg_replace, and this is what I came up with when trying to replace hyperlinks and email-addresses in strings, with actual links.

    The function works with http://, https:// and ftp:// links, with or without the 'www'. Also works with www links without the http:// part, links with trailing punctuation, links without leading whitespace, 'weird' links (ie 'http://foo.bar/index.php?foo=bar&foobar=barfoo') etc.

    Give it a shot :-)


    function urltext($str) {
    $str = ereg_replace("((www.)([a-zA-Z0-9@:%_.~#-\?&]+[a-zA-Z0-9@:%_~#\?&/]))", "http://\\1", $str);
    $str = ereg_replace("((ftp://|http://|https://){2})([a-zA-Z0-9@:%_.~#-\?&]+[a-zA-Z0-9@:%_~#\?&/])", "http://\\3", $str);
    $str = ereg_replace("(((ftp://|http://|https://){1})[a-zA-Z0-9@:%_.~#-\?&]+[a-zA-Z0-9@:%_~#\?&/])", "[<A HREF=\"\\1\" TARGET=\"_blank\">\\1</A>]", $str);
    $str = ereg_replace("([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})","[<A HREF=\"mailto:\\1\">\\1</A>]", $str);
    return $str;
    }


  5. #5
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That script is kinda stupid because it converts all links to http://, even if it initially was ftp:// or https://.

  6. #6
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *sigh* the problems I create by blindly posting other people's regex code without looking too carefully

    I did just try it out though, and http://, ftp:// and https:// links work ok.. If you're talking about the 2nd ereg_replace, I agree with you too.. it looks like it would convert them all to http://..

    here's the output from my quick test (using htmlspecialchars so we can see the full output)..

    http://www.domain.com - [<A HREF="http://www.domain.com" TARGET="_blank">http://www.domain.com</A>]
    www.domain.com - [<A HREF="http://www.domain.com" TARGET="_blank">http://www.domain.com</A>]
    domain.com - domain.com
    ftp://some.domain.com - [<A HREF="ftp://some.domain.com" TARGET="_blank">ftp://some.domain.com</A>]
    https://some.domain.com - [<A HREF="https://some.domain.com" TARGET="_blank">https://some.domain.com</A>]



    It didn't convert the domain.com, but we could fix that by adding

    $str = ereg_replace("(([a-zA-Z0-9@:%_.~#-\?&]+[a-zA-Z0-9@:%_~#\?&/]))", "http://\\1", $str);

    at the top of the function...

  7. #7
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, and the code I used to generate the test there..

    echo "http://www.domain.com - " . htmlspecialchars(urltext("http://www.domain.com"));
    echo "\n<br>www.domain.com - " . htmlspecialchars(urltext("www.domain.com"));
    echo "\n<br>domain.com - " . htmlspecialchars(urltext("domain.com"));
    echo "\n<br>ftp://some.domain.com - " . htmlspecialchars(urltext("ftp://some.domain.com"));
    echo "\n<br>https://some.domain.com - " . htmlspecialchars(urltext("https://some.domain.com"));

  8. #8
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies everyone !


Bookmarks

Posting Permissions

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