PHP preg_split 无论如何都要保留分隔字符串?
如果我有以下字符串,
$string = "
0001
a
b
c
d
e
0002
f
g
h
i
j
";
我将其与 $r = preg_split("/\d{4}\n/", $string);
分开,效果很好,但我也想包括分隔字符串..
即目前这给了我
Array [0] {
a
b
c
d
e
}
=> [1] {
f
g
h
i
j
}
,但我希望它包括分隔符,即
Array [0] {
0001
a
b
c
d
e
}
=> [1] {
0002
f
g
h
i
j
}
希望这是有道理的..
谢谢
李
If i have the following string
$string = "
0001
a
b
c
d
e
0002
f
g
h
i
j
";
I am splitting it with $r = preg_split("/\d{4}\n/", $string);
and that works fine but i also want to include the dividing string..
i.e. at the moment this gives me
Array [0] {
a
b
c
d
e
}
=> [1] {
f
g
h
i
j
}
but i want it to include the divider i.e.
Array [0] {
0001
a
b
c
d
e
}
=> [1] {
0002
f
g
h
i
j
}
Hope that makes sense..
Thanks
Lee
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 lookahead [docs]< /a>:
DEMO
不知道为什么你有
中你的表情。您的字符串中没有 HTML(至少在本例中没有)。You could use a lookahead [docs]:
DEMO
Don't know why you have
<br/>
in your expression. There is no HTML in your string (at least not in this example).使用 preg_match_all 代替。
匹配项将包含字符串中以 4 位数字开头并在下一个数字之前结束的所有部分。
您还可以使用括号过滤掉
。Use preg_match_all instead.
The matches will contain all the parts of your string that start with 4 digits and end right before the next digit.
You also may filter out the
<br>
using brackets.