dqf60304 2015-02-06 08:32
浏览 363
已采纳

如何使用php preg_split分割字符串并保留部分使用的分隔符?

I need to use preg_split to split the string below by using "};{" as the delimiter. But I want to keep/return the } and { in the returned array.

Regex can do so much, but wondering if this is possible using preg_split.

{'aid':10,'aSL'{'ac':true,'g_ac':true,'stats':0,'ldb':0,'g_':0},'hs':'4.8','l_p':1229241600},{'aid':80,'aSL':{'ac':0,'g_ac':0,'stats':0,'ldb':0,'g_':0}},{'aid':100,'aSL':{'ac':0,'g_ac':0,'stats':0,'ldb':0,'g_':0}},{'aid':30,'aSL':{'ac':true,'g_ac':true,'stats':0,'ldb':0,'g_':0}},{'aid':40,'aSL':{'ac':0,'g_ac':0,'stats':0,'ldb':0,'g_':0}},{'aid':60,'aSL':{'ac':true,'g_ac':true,'stats':0,'ldb':0,'g_':0}}

I want it to return like this

[0] => {'aid':10,'aSL':{'ac':true,'g_ac':true,'stats':0,'ldb':0,'g_':0},'hs':'4.8','l_p':1229241600}
[1] => {'aid':80,'aSL':{'ac':0,'g_ac':0,'stats':0,'ldb':0,'g_':0}
[2] => {'aid':100,'aSL':{'ac':0,'g_ac':0,'stats':0,'ldb':0,'g_':0}
[3] => {'aid':30,'aSL':{'ac':true,'g_ac':true,'stats':0,'ldb':0,'g_':0}
[4] => {'aid':40,'aSL':{'ac':0,'g_ac':0,'stats':0,'ldb':0,'g_':0}
[5] => {'aid':60,'aSL':{'ac':true,'g_ac':true,'stats':0,'ldb':0,'g_':0}
  • 写回答

2条回答 默认 最新

  • douluchuo0801 2015-02-06 08:40
    关注

    To not include } { in the split-sequence, could use lookarounds instead:

    (?<=}),(?={) or (?<=});(?={)

    Test at regex101.com


    For use with the regex-functions need to put that into delimiters: /pattern/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部