加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP – Preg_match最短匹配

发布时间:2020-12-13 16:58:27 所属栏目:PHP教程 来源:网络整理
导读:我正试图从这个字符串中获
我正试图从这个字符串中获取“内容”:

$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('/starth*((?:(?!start).)*)end/',$matches);
echo $matches[1];
// content

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读