PHP Get string after a specific regex pattern match

烈酒焚心 提交于 2019-12-12 05:25:07

问题


I am using the regex pattern from this SO thread: https://stackoverflow.com/a/11416262/

In the email's body message, there can be multiple emails so I am wanting to grab the email after a specific string:

The following users in your Google Apps domain appear to be affected: name@domain.com ....

Here is what I am doing:

$message = (imap_fetchbody($inbox,$email_number,1.2)); 
if($message == '')
{
    $message = imap_fetchbody($inbox,$email_number,1);
}
// Text inside pattern can be made smaller too (i.e. from 'Google' to 'affected:')
$pattern = '/(?<=The following users in your Google Apps domain appear to be affected: )\S+/i';
preg_match($pattern, $message, $match);

When I get the output of $message and check the pattern on this website: http://www.phpliveregex.com/ It works (shows the email), but it does not work in my code.

var_dump($match) shows

array (size=0)
  empty

I'd like to grab just the email in the body and use it later.

Edit #1:

if (preg_match('/affected:/',$message)
    echo 'true';

returns true, but it does not if it is '/affected: /' (with space). The gettype of $message is a string, but I am wondering if there's are new line characters too. Here is the real email view: Real email sample

Edit #2: Sorry for misleading/not providing the real information (my first time working with imap). The issue was the newline characters that were added. I ended up replacing all newline instances with ' '. Now,

if (preg_match('/The following users in your Google Apps domain appear to be affected: /',$string))
    echo 'true';

returns true, but my bigger regex still returns var_dump as empty.

Edit #3: With a little testing, I had to add a few more spaces at the end to make it go from "affected: " to "affected: ". If you can help make the regex a little cleaner, please do so.

Thank you for all the help!

来源:https://stackoverflow.com/questions/31057201/php-get-string-after-a-specific-regex-pattern-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!