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

php – 匹配模式并使用preg_match_all排除子串

发布时间:2020-12-13 22:48:20 所属栏目:PHP教程 来源:网络整理
导读:我需要找到START和END之间的所有字符串,包括匹配字符串中的PADDING子字符串.我发现的最好方法是 $r="stuffSTARTthisPADDINGisENDstuffstuffSTARTwhatPADDINGIwantPADDINGtoPADDINGfindENDstuff" ;preg_match_all('/START(.*?)END/',str_replace('PADDING',''
我需要找到START和END之间的所有字符串,包括匹配字符串中的PADDING子字符串.我发现的最好方法是

$r="stuffSTARTthisPADDINGisENDstuffstuffSTARTwhatPADDINGIwantPADDINGtoPADDINGfindENDstuff" ;
preg_match_all('/START(.*?)END/',str_replace('PADDING','',$r),$m);
print(join($m[1]));
> thisiswhatIwanttofind

我想用尽可能最小的代码大小来做这个:只有preg_match_all和没有str_replace的更短,最终直接返回没有连接数组的字符串?我试过一些环视表达式,但我找不到合适的表达式.

解决方法

$r="stuffSTARTthisPADDINGisENDstuffstuffSTARTwhatPADDINGIwantPADDINGtoPADDINGfindENDstuff";
echo preg_replace('/(END.*?START|PADDING|^[^S]*START|END.*$)/',$r);

这应该使用单个正则表达式模式返回给你这个Iwanttofind

说明:-

END.*?START  # Replace occurrences of END to START
PADDING      # Replace PADDING
^[^S]*START  # Replace any character until the first START (inclusive)
END.*$      # Replace the last END and until end of the string

(编辑:李大同)

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

    推荐文章
      热点阅读