#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Extracting email headers


    I am using php and was wondering if anyone has a regex for extracting the originating IP address out of an email header?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    I have done some testing and realized there are just too many variables for me to do this. It's probably possible I just don't have the regex knowledge to do it. I'm sure it could be done with a combination of if's/else's, preg_match, and regular expressions.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    837
    Rep Power
    496
    Hi,

    validating IP addresses can be very tricky, but just finding them is probably relatively straight forward.

    I don't know the specifics of PHP, so just give you the basic regex that would work in Perl and presumably should work in PHP (with possibly minor adjustments):

    /((\d{1,3}\.){3}\d{1,3})/

    The basic pattern is \d{1,3}\., which says 1 to 3 digits followed by a dot. I repeat this pattern 3 times by using the quantifier {3} on it, and then add a pattern for matching 3 digits without a dot.

    It could optionally be refined by using with non capturing parentheses for the repeated pattern if PHP supports this.

IMN logo majestic logo threadwatch logo seochat tools logo