Using regex in a string for strpos()

前端 未结 2 2109
眼角桃花
眼角桃花 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.

    0 讨论(0)
  • 2021-02-19 00:45

    You can use preg_match instead of strpos for regex

    preg_match (regex, $string, $matches, PREG_OFFSET_CAPTURE);
    
    PREG_OFFSET_CAPTURE gives you the position of match.
    
    0 讨论(0)
提交回复
热议问题