如何在php中的某个字符串之后选择一个字符串


how to select a string after a certain string in php

我正在制作一个脚本,用于收集通过电子邮件发送的备份信息。

现在我正试图让它在电子邮件正文中搜索一个字符串,然后在该字符串后面选择一些单词。

前任。备份:成功/备份:失败

我需要在"备份:"之后得到什么

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

但后来我得到了这个错误:Notice: Undefined offset: 1 in C:'Users'stagiair'Downloads'USBWebserver v8.5'USBWebserver v8.5'8.5'root'index.php on line 50

代码:

  <?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);
?>

谨致问候,lars-kapstein

您需要捕获组:

preg_match('/(?<=backup: )('S+)/i', $output, $match);
//                here  __^ __^

您可以使用以下regex进行preg_match调用:

(?<=backup: )'w+

现场演示:http://www.rubular.com/r/8cOLjuUOQS

substr(strstr($output,":"),1);