PHP and email addresses

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

    PHP and email addresses

    The expression:

    preg_match_all( "/text.*?([A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6})/i",$f,
    $matches);

    Returns ONE email address on a line in $f beginning 'text' - how do I
    retrieve ALL the email addresses on a line in $f beginning 'text' in php?

    Many thanks

    Nick
  • Rik

    #2
    Re: PHP and email addresses

    Nick Bell wrote:[color=blue]
    > The expression:
    >
    > preg_match_all( "/text.*?([A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6})/i",$f,
    > $matches);
    >
    > Returns ONE email address on a line in $f beginning 'text' - how do I
    > retrieve ALL the email addresses on a line in $f beginning 'text' in
    > php?
    >
    > Many thanks[/color]

    Without looking further at e-mail validation, adjust:
    "/text.*?([A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6})/i"
    to:
    "/^text(:?.*?([A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}))*/i"

    Note that your regex will think .@..xx a valid email-adress.

    Grtz,
    --
    Rik Wasmus


    Comment

    • Nick Bell

      #3
      Re: PHP and email addresses

      Rik wrote:[color=blue]
      > Nick Bell wrote:[color=green]
      >> The expression:
      >>
      >> preg_match_all( "/text.*?([A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6})/i",$f,
      >> $matches);
      >>
      >> Returns ONE email address on a line in $f beginning 'text' - how do I
      >> retrieve ALL the email addresses on a line in $f beginning 'text' in
      >> php?
      >>
      >> Many thanks[/color]
      >
      > Without looking further at e-mail validation, adjust:
      > "/text.*?([A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6})/i"
      > to:
      > "/^text(:?.*?([A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}))*/i"
      >
      > Note that your regex will think .@..xx a valid email-adress.
      >
      > Grtz,[/color]
      Thanks. The $f is the result of file_get_conten ts(...) and the
      'beginning of line' function ^ doesn't seem to detect the beginnings of
      lines generated using this method.

      Any thoughts?

      Nick

      Comment

      • Rik

        #4
        Re: PHP and email addresses

        Nick Bell wrote:[color=blue]
        > Rik wrote:[color=green]
        >> Nick Bell wrote:[color=darkred]
        >>> The expression:
        >>>
        >>> preg_match_all( "/text.*?([A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6})/i",$f,
        >>> $matches);
        >>>
        >>> Returns ONE email address on a line in $f beginning 'text' - how do
        >>> I retrieve ALL the email addresses on a line in $f beginning 'text'
        >>> in php?
        >>>
        >>> Many thanks[/color]
        >>
        >> Without looking further at e-mail validation, adjust:
        >> "/text.*?([A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6})/i"
        >> to:
        >> "/^text(:?.*?([A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}))*/i"
        >>
        >> Note that your regex will think .@..xx a valid email-adress.
        >>
        >> Grtz,[/color]
        > Thanks. The $f is the result of file_get_conten ts(...) and the
        > 'beginning of line' function ^ doesn't seem to detect the beginnings
        > of lines generated using this method.[/color]

        First: ^isn't a function.
        Second: then try without:
        "/text(:?.*?([A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}))*/i"

        Grtz,
        --
        Rik Wasmus


        Comment

        • Tim Roberts

          #5
          Re: PHP and email addresses

          Nick Bell <me@privacy.net > wrote:
          [color=blue]
          >The expression:
          >
          >preg_match_all ("/text.*?([A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6})/i",$f,
          >$matches);
          >
          >Returns ONE email address on a line in $f beginning 'text' - how do I
          >retrieve ALL the email addresses on a line in $f beginning 'text' in php?[/color]

          This isn't foolproof by a long shot. Every printable character except [,
          \, and ] is valid in an RFC2822 e-mail address, and even [ and ] can be
          used if the address is wrapped in quotes.

          The full regular expression required to match an RFC2822 compliant e-mail
          address is more than 6,000 characters long, and is one of the gems in
          Friedl's book "Mastering Regular Expressions".

          It is common to use "*" in an e-mail address, for example. Sendmail has a
          feature that uses it. If you send to "abc*def@joe.co m", it will try find
          an alias for "abc*def" exactly. Failing that, it will send the message to
          "abc@joe.co m". This is great for spam catching; I can sign up with web
          sites as "timr*121@probo .com" without defining it, and if I get to much
          spam, I just add a specific alias for that which routes to null.
          --
          - Tim Roberts, timr@probo.com
          Providenza & Boekelheide, Inc.

          Comment

          Working...