Search code examples
phppreg-match

Fetch the String + bounced email correctly


I've beeing starting to work with preg_match and for that I've beeing trying to fetch a exactly part of the text with an email on it. However I haven't had success on this task. Bellow is the text and also the preg_match code I've being trying to use.

The Text is this one bellow:


From: testemail@yahoo.de
Reply-To: testemail@yahoo.de
To: "fighfo@dihfodf.dfihdofd"


What I want to fetch:


To: "fighfo@dihfodf.dfihdofd"


And what I've being using so far:


preg_match("/To: \"\b([A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,})\b\"/i", $body, $matches)


Please, Does anyone have any idea on how this issue could be solved.

I thank you in advance.

Best regards,

M.J.


Solution

  • well you can do it like this too:

    Read string line by line, if first 3 characters match To:, that's that line you are looking for then.

    foreach(preg_split("/((\r?\n)|(\r\n?))/", $subject) as $line){
         if($line[0]=='T' && $line[1]=='o' && $line[2]==':'){
           // $line is the line that you are looking for
        }
    }