regex negative look behind not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • brendan

    #1

    regex negative look behind not working

    Hi ... want to antispam any email address that is written in a page, so long
    as it is not already contained in a hyperlink ie 'mailto:user@em ail.com'

    so am trying to use a negative look behind but I can't get it to work

    preg_replace('/(?<!mailto)(([_\\.0-9a-z-]+)@([_\\.0-9a-z-]+))/i',
    "<script>docume nt.write('<a
    href=\'mailto:' +'\\1'+'@'+'\\2 '+'\'>'+'\\1'+' @'+'\\2'+'</a>');</script>",$st
    ring );

    doesn't ignore strings starting with mailto (have also tried using
    'mailto\\:'


  • Chung Leong

    #2
    Re: regex negative look behind not working

    That doesn't work, because the pattern will match again as soon as the
    regexp engine looks one letter to the right.

    The bracketed letter below indicates the first letter of a possible
    match. The following fails the lookbehind assertion, because "mailto:"
    matches the condition:

    mailto:[c]hernyshevsky@ho tmail.com

    The following does not fail the assertion, as "mailto:c" is not a
    match:

    mailto:c[h]ernyshevsky@hot mail.com


    A job like this is easier with preg_replace_ca llback(), arguably the
    most powerful text manipulation function in PHP. Example:

    function antispam($m) {
    return ($m[1]) ? $m[0] :
    "<script>docume nt.write('<a
    href=\'mailto:' +'{$m[3]}'+'@'+'{$m[4]}'+'\'>'+'$m[3]'+'@'+'$m[4]'+'</a>');</script>";
    }

    preg_replace_ca llback('/(mailto:)?\s*(([_\\.0-9a-z-]+)@([_\\.0-9a-z-]+))/i',
    'antispam', $string);

    Comment

    • brendan

      #3
      Re: regex negative look behind not working


      "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
      news:1126644521 .260826.103720@ g44g2000cwa.goo glegroups.com.. .[color=blue]
      > That doesn't work, because the pattern will match again as soon as the
      > regexp engine looks one letter to the right.[/color]
      [color=blue]
      > A job like this is easier with preg_replace_ca llback(), arguably the
      > most powerful text manipulation function in PHP. Example:
      >
      > function antispam($m) {
      > return ($m[1]) ? $m[0] :
      > "<script>docume nt.write('<a
      >[/color]
      href=\'mailto:' +'{$m[3]}'+'@'+'{$m[4]}'+'\'>'+'$m[3]'+'@'+'$m[4]'+'</a>');</
      script>";[color=blue]
      > }
      >
      >[/color]
      preg_replace_ca llback('/(mailto:)?\s*(([_\\.0-9a-z-]+)@([_\\.0-9a-z-]+))/i',[color=blue]
      > 'antispam', $string);
      >[/color]
      Excellent, this works perfectly.


      Comment

      Working...