Hello and first all happy 2nd xmas day.

I'm trying to convert all urls that aren't surrounded by double-quotes to hyperlinks... no matter what I do I don't get the proper result, either I match every url or nothing. 🙁

Any help would be great, cheers!

    /bump

    Really tried everything I could but when I put an url regex between (?<!") and (?!") then nothing is matched:

    $sData = preg_replace('/(?<!")(?!")/si','<a href=\"$1\">$1</a>',$sData);

    Anyone???

      Pfff still not working, here are some other examples of what I tried:

      $bla = preg_replace('/(?<!")(http|https|www)([[:alnum:]/\n+-=%&:.~?][<>[:space:]]+[#[:alnum:]+])(?!")/','<a href=\'\2\3\' target=\'_blank\'>\2\3</a>',$bla);


      This one comes closest but unfortunately it screwes up other tags:


      $bla = ereg_replace('"([[:alnum:]/\n+-=%&:_.~?][<>[:space:]]+[#[:alnum:]+]*)["]','<a href=\'\1\2\' target=\'_blank\'>\1\2</a>',$bla);


      Tried all kinds of things for hours but I'm sorry to say that I suck @ regexes. 🙁

        Use code, html or php tags for code, not quote tags.

        You should not use ereg since it's been deprecated and will be removed as of PHP 6.

        Since you use / as pattern delimiter, you'd have to escape every / in the pattern. For readability it would make sense to choose a delimiter which does not appear in the pattern instead.

        Your pattern seems odd to me in this place

        tablex;10937590 wrote:

        ([[:alnum:]/\n+-=%&:_.~?]
        [<>[:space:]]+[#[:alnum:]+]*)

        The first line matches one character among those specified, followed by any amount of characters not among those specified on the second line. Why not just go for one or more of the characters from the first line.

        Also, asserting that the string is not preceeded by " means little when you allow either http or www in some cases

        "http://www.example.com"

        First it finds that http is precceded by " so this doesn't match. Then it finds that www is not preceeded by ", since it is preceeded by / which means this is a match. With this approach, you'd need to assert that www is not precceded by http:// or https:// (and here you can't use s* since assertions need patterns of fixed length).

        Following your pattern as a starting point, I came up with this

        $pattern = '#(?<!")(https*://|(?<!http://|https://)www)([[:alnum:]/\n+-=%&:_.~?]+)#';
        $replace = '<a href="\1\2" target="_blank">\1\2</a>';
        

        The above should more or less do what you want. But don't forget that matching www.example.com would create a link with a relative URI, i.e. http://yourserver.com/www.example.com, and not http://www.example.com.

        But I'd personally prefer to use a positive assertion for any whitespace before the would be link since it simplifies the pattern and stands to reason that the link would be a separate "word", and not just a part thereof: astrangestringhttp://shouldireallybelinked

        $pattern = '#(?<=\s)(https*://|www)([[:alnum:]/\n+-=%&:_.~?]+)#';
        $replace = '<a href="\1\2" target="_blank">\1\2</a>';
        

        If you want absolute URIs even when just www.example.com is matched, I assume it would be possible to create a pattern that adds http:// when only www is matched at the start of the string, but if this is what you want it would be easier to use two patterns. One starting with https* and used as above. The other starting with www and having the replacement string start be [url]http://\1\2[/url].

          Write a Reply...