PHP REGEX - 通过 preg_split 在换行符处将文本转换为数组
已编辑:
需要有关拆分数组
数组示例的帮助:
array (
[0] =>
:some normal text
:some long text here, and so on... sometimes
i'm breaking down and...
:some normal text
:some normal text
)
好的,现在通过使用
preg_split( '#\n(?!s)#' , $text );
i get
[0] => Array
(
[0] => some normal text
[1] => some long text here, and so on... sometimes
[2] => some normal text
[3] => some normal text
)
我想要得到这个:
[0] => Array
(
[0] => some normal text
[1] => some long text here, and so on... sometimes i'm breaking down and...
[2] => some normal text
[3] => some normal text
)
什么 Regex 可以获取整行并在换行符处拆分! ?
EDITED:
need help on split Array
array example:
array (
[0] =>
:some normal text
:some long text here, and so on... sometimes
i'm breaking down and...
:some normal text
:some normal text
)
ok, now by using
preg_split( '#\n(?!s)#' , $text );
i get
[0] => Array
(
[0] => some normal text
[1] => some long text here, and so on... sometimes
[2] => some normal text
[3] => some normal text
)
I want get this:
[0] => Array
(
[0] => some normal text
[1] => some long text here, and so on... sometimes i'm breaking down and...
[2] => some normal text
[3] => some normal text
)
what Regex can get the entire line and also split at line break!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“换行符”定义不明确。 Windows 仅使用 CR+LF (\r\n)、Linux LF (\n)、OSX CR (\r)。
preg_* 常规异常中有一个鲜为人知的特殊字符 \R 与所有三个匹配:
"line break" is ill-defined. Windows uses CR+LF (\r\n), Linux LF (\n), OSX CR (\r) only.
There is a little-known special character \R in preg_* regular exceptions that matches all three:
这是一个有效的示例,即使您在字符串中嵌入了冒号字符(但不在行首):
结果:
Here's an example that works, even if you have a colon character embedded inside the string (but not at start of the line):
result:
file()
将文件读入一个数组。file()
reads a file into an array.如果您在 : 字符上拆分数组..
If you split the array on the : character..