1

I'm making a script that collects backup information that has been sent trough email.

Now I'm trying to get it to search for a string in the email body and then select some words behind that string.

ex. backup: successful / backup: failed

I need to get whats after the "backup:"

I tried:preg_match('/(?<=backup: )\S+/i', $output, $match); echo $match[1];

But then I get this error: Notice: Undefined offset: 1 in C:\Users\stagiair\Downloads\USBWebserver v8.5\USBWebserver v8.5\8.5\root\index.php on line 50

the code:

  <?php
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '**@***.**';
$password = '******';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'BODY "backup: geslaagd"');

/* if emails are returned, cycle through each... */
if($emails) {

    /* begin output var */
    $output = '';

    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    foreach($emails as $email_number) {

        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $message = imap_fetchbody($inbox,$email_number,2);

        /* output the email header information */
        $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
        $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
        $output.= '<span class="from">'.$overview[0]->from.'</span>';
        $output.= '<span class="date">on '.$overview[0]->date.'</span>';
        $output.= '</div>';

        /* output the email body */
        $output.= '<div class="body">'.$message.'</div>';
    }

    echo $output;
    preg_match('/(?<=backup: )\S+/i', $output, $match);
    echo $match[1];

} 

/* close the connection */
imap_close($inbox);
?>

Kind regards, lars kaptein

1
  • 1
    post the code, and line 50 Commented Jul 8, 2013 at 11:23

3 Answers 3

2

You need to capture the group:

preg_match('/(?<=backup: )(\S+)/i', $output, $match);
//                here  __^ __^
Sign up to request clarification or add additional context in comments.

3 Comments

so like this? preg_match((?<=backup: )\w+, $output, $match);echo $match[1];
@LarsKaptein: Sorry, i don't get you. Just use what I said and it should work.
yeah sorry, posted the comment to the wrong person but it wouldn't let me delete it.. but stil Undefined offset: 1 on the line of the echo
1

You can use following regex for preg_match call:

(?<=backup: )\w+

Live Demo: http://www.rubular.com/r/8cOLjuUOQS

5 Comments

So like this? preg_match((?<=backup: )\w+, $output, $match); echo $match[1];
echo $match[0]; will do the job since I am not really capturing string after backup: in groups.
There is a syntax error in your regex as regex needs boundaries. You need to use: preg_match('/(?<=backup: )\w+/', $output, $match);
still Undefined offset: 1 on the line of the echo :( and if i change the echo $match[1] to $match[0] Undefined offset: 0
Make sure you're using echo $match[0]; with my regex above. You can see demo code as provided above.
0
substr(strstr($output,":"),1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.