PHP 正则表达式从开始到第一次出现匹配


PHP Regex Match from Start to First Occurrence

>我有一个字符串说$str - This is my test string that needs to be matched and not the test keywords following it

输出 - This is my test

但是我的正则表达式模式:(.*?测试

匹配到最后。请帮助如何实现相同的目标

您可以使用这个简单的正则表达式:

^.*?test

或者最好不要使用正则表达式并使用strpos字符串函数。

$pos = strrpos($str, "test", 0);
if ($pos === true) {
    // found...
}