Using regex in a string for strpos()

前端 未结 2 2108
眼角桃花
眼角桃花 2021-02-19 00:18

I want to get the scripts to search the $open_email_msg which different e-mails will have different info but the same format as below.

I haven\'t really used regex much

2条回答
  •  离开以前
    2021-02-19 00:42

    Try using the PREG_OFFSET_CAPTURE flag for preg_match. Something like this:

    preg_match('/Title: .*/', $open_email_msg, $matches, PREG_OFFSET_CAPTURE);
    echo $matches[0][1];
    

    This should give you the initial position of the string.

    Note that the regex I'm using could be wrong and not take into account line endings and stuff, but that's another subject. :)

    EDIT. A better solution for what you want (if I understand it correctly) would be something like this:

    $title = preg_match('/Title: (.*)/', $open_email_msg, $matches) ? $matches[1] : '';
    

    You'd then get the title into the $title variable, and an empty string if no title was found.

提交回复
热议问题