I searched the board for this, but I didn't know exactly what to search for, so I didn't find anything.

My company runs different ads that people can submit to for free.
Some of these people may want to include links, so I was wondering how to do it automatically...

example - person types this in :

"Check out my website at www.website.web"

And I want it to automatically place the address into A HREF tags.. is the best way to do this with regular expressions? If so,
I am not too good with regexps yet...

thanks in advance!

😃

    Maybe I am just a little challenged...
    but here is what I have right now :

      $desc = eregi_replace(
        "(http|https|ftp)://([[:alnum:]/\n+-=%&:_.~?]+[#[:alnum:]+]*)",
        "<a href=\"\\1://\\2\" target=\"_blank\">\\1://\\2</a>",
        $desc);	
    

    That works great if the user enters [url]http://[/url] etc...
    but I want to modify this regexp to work with just a www. (my boss says if it's not a www site, just screw 'em)

    Anyway, I appreciate it, and maybe it'll be easier to see what I am working with?

      2 months later

      From what I saw in the manual, they have an example of using ereg_replace that works... here it is:

      $desc = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\">\\0</a>", $desc);

      just change ereg_replace with eregi_replace 🙂

        Firstly i answered this question on the forums twice in the last month. There is this little button at the top called search, it's also linked to in my signature.

        Secondly the answer is given in an example in the manual.

        So next time STFF and RTFM before you post a question.

          also watch for \0 in double-quoted strings, that's ASCII 0 really, you want \0 (or just \0 in single-quoted strings) (come to think of it its probably the forums fault and there were two slashes there)

          but drawmack's right, thsi has been answered over and over, and going back a few pages of threads would probably come up with something, but he did say he searched so i'm willing to help him... people do have a really obscure way of describing things that differs greatly person-to-person.

            The best method in my opinion is the following(uses a function):

            function make_clickable($text)
            {
            
            // pad it with a space so we can match things at the start of the 1st line.
            $ret = ' ' . $text;
            
            // matches an "xxxx://yyyy" URL at the start of a line, or after a space. 
            // xxxx can only be alpha characters. 
            // yyyy is anything up to the first space, newline, comma, double quote or < 
            $ret = preg_replace("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret); 
            
            // matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing 
            // Must contain at least 2 dots. xxxx contains either alphanum, or "-" 
            // zzzz is optional.. will contain everything up to the first space, newline, 
            // comma, double quote or <. 
            $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret); 
            
            // matches an email@domain type address at the start of a line, or after a space.
            // Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
            $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
            
            // Remove our padding..
            $ret = substr($ret, 1);
            
            return($ret);
            }
            
            $url = "www.google.com";
            
            $url = make_clickable($url);
            
            
              Write a Reply...