Using regex in a string for strpos()

◇◆丶佛笑我妖孽 提交于 2019-12-03 22:53:56

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.

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