Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post

    link finder regex

    PHP Code:
    function findLinks($string){
        
    // link URLs
        
    $string =
    eregi_replace("(([[:alnum:]]+://)|www\.)([^[:space:]]*)([[:alnum:]#?/&=])""<a target=\"_blank\" href=\"\\1\\3\\4\" title=\"Go to \\1\\3\\4\">\\1\\3\\4</a> "$string);

        
    // fix lazy URLs
        
    $string eregi_replace("href=\"www","href=\"http://www"$string);

        
    // link mailtos
        
    $string eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))",
     
    "<a href=\"mailto:\\1\" title=\"E-mail \\1\">\\1</a> "$string);

        
    // truncates long urls that can cause display problems
        
    $string =
    preg_replace("/>(([[:alnum:]]+:\/\/)|www\.)([^[:space:]]{30,40})([^[:space:]]*)([^[:space:]]{10,20})([[:alnum:]#?\/&=])</",
     
    ">\\1\\3...\\5\\6<"$string);

        
    $string stripslashes($string);
        return 
    $string;
        } 
    this seems to work really well as long as someone has a space after the URL (it doesnt work if someone has a linebreak after a URL)... it just doesnt put </a> before <br>

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Wow, that is the most elaborate link finder I have seen. Looks like wasted effort. It's also old (since it uses eregi_replace).

    Search these boards, this question has been asked many times before. Or use a search engine. There are much better solutions to this problem. This one ranks low on the scale.

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    i have searched many times on google and this one was the only one that worked with almost any link type... other ones are simpler but they dont detect every link submitted.


  •  

    Posting Permissions

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