Hello, everyone, I have this regex which matches full and partial URLs and converts them to links:

$regex = '(http(s?)://)?(([[:alpha:]]+\.)+(com|org|edu|net|gov)(/[^[:space:]]*)?)';
$text = eregi_replace($regex, "<a href=\"http\\2://\\3\" target=\"_blank\">\\1\\3</a>", $text);

Now I need to modify it so that if there is an @ before the partial URL, it does not match. (I have a separate eregi_replace() for doing emails, obviously). I tried adding (?<!@) right before the first set of ((. (Also, then my \3 change to \4) When I did this, it gives me the error: Warning: eregi_replace() [function.eregi-replace]: REG_BADRPT in /srv/test/htdocs/convert_urls/convert.php on line 33. Any ideas what I'm doing wrong?

Thanks,
Jonathon Reinhart

    To restate, what is the problem with this:

    //                      vvvvvv - Negative lookbehind for @
    $regex = '(http(s?)://)?(?<!@)(([[:alpha:]]+\.)+(com|org|edu|net|gov)(/[^[:space:]]*)?)';
    $text = eregi_replace($regex,
        "<a href=\"http\\2://\\4\" target=\"_blank\">\\1\\4</a>",
        $text);

      I do not believe that the ereg set of functions support assertions. You should probably look into using the PCRE functions (preg_*() functions), which in any case are generally recommended now over the ereg functions both for features and performance.

        Is there anything in my first regex that will not work with preg?

          I do not believe preg supports the posix-style character classes using the "[:xxx]" format. The following should be equivalent:

          $regex = '#(http(s?)://)?(([a-z]+\.)+(com|org|edu|net|gov)(/\S*)?)#i';
          

          ("\S" = any non-whitespace character)

            preg regexes require start/end delimiters. The "classic" delimiter is the forward slash, but using it requires that you escape any literal slashes in the regex itself. Since there were no "#" characters in the regex, I decided to use "#" as the delimiter, instead. After the closing delimiter, you can add any of a number of modifiers. the "i" modifier means to do a case-insensitive comparison.

            PCRE pattern modifiers
            PCRE pattern syntax

              Also note that the ereg set of functions should never be used - they've been deprecated in favor of the PCRE functions (and are no longer available as of PHP6, IIRC).

                5 days later
                Write a Reply...