PHP – Preg_match最短匹配

我正试图从这个字符串中获取内容”:

$string = "start start content end";

像preg_match这样:

preg_match('/start(.*?)end/', $string, $matches);
echo $match[1];

但是,问题是$matches [1]返回开始内容不仅仅是内容,因为$string中有两个开头(也许更多)

如何只使用preg_match获取内容部分?

解决方法:

使用否定前瞻:

$string = "start start content end";
preg_match('/start\h*((?:(?!start).)*)end/', $string, $matches);
echo $matches[1];
// content

(?:(?!start).)将匹配任何字符,如果没有后跟start.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...