سؤال

I have an email saved in a file

Date: Wed, 9 Apr 2014 14:33:11 +0000
Return-Path: x.com
To: personsEamilAddress@email.com
From: x
Subject: Hello world
Message-ID: <x>
X-Priority: 3
X-Mailer: PHPMailer 5.1 (phpmailer.sourceforge.net)
Sender: senderEmail@mail.com
MIME-Version: 1.0
Content-Type: text/html; charset="iso-8859-1"
X-SES-Outgoing: 2014.04.09-54.240.8.56
Content-Transfer-Encoding: quoted-printable

I'm trying to work out who the email was sent to? ie personsEamilAddress@email.com

I've tried

preg_match_all('/To: (.*?)/', $subject, $result, PREG_SET_ORDER);
print_r($result);

But the array that is printed out doesn't contain the value of the email address.

Array
(
    [0] => Array
        (
            [0] => To: 
            [1] => 
        )

    [1] => Array
        (
            [0] => To: 
            [1] => 
        )

)
هل كانت مفيدة؟

المحلول

Your problem is the ? in your search group.

Also, the s modifier at the end let's you select everything with the . including line breaks and whitespaces.

The Solution therefore would be:

%^To: (.*)%m

The m Modifier causes ^ and $ to match the begin/end of each line (not only begin/end of string)

نصائح أخرى

Have you tried with:

preg_match_all('/To: (.*)?/', $subject, $result, PREG_SET_ORDER);
print_r($result);

It should return:

Array
(
[0] => Array
    (
        [0] => To: personsEmailAddress@email.com
        [1] => personsEmailAddress@email.com
    )
)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top