PHP中使用正则表达式将多行字符串转换为多元素数组
我需要拆分以下字符串并将每个新行放入新的数组元素中。
this is line a.(EOL chars = '\r\n' or '\n')
(EOL chars)
this is line b.(EOL chars)
this is line c.(EOL chars)
this is the last line d.(OPTIONAL EOL chars)
(请注意,最后一行可能不存在任何 EOL 字符。该字符串有时也只包含 1 行,根据定义,这是最后一行。)
必须遵循以下规则:
- 应丢弃空行(如第二行)并没有放 进入数组。
- 不应包含 EOL 字符,否则 我的字符串比较失败。
所以这应该会产生以下数组:
[0] => "this is line a."
[1] => "this is line b."
[2] => "this is line c."
[3] => "this is the last line d."
我尝试执行以下操作:
$matches = array();
preg_match_all('/^(.*)$/m', $str, $matches);
return $matches[1];
$matches[1] 确实包含每个新行,但是:
- 还包含空行
- 似乎最后会走私一个 '\r' 字符数组中的字符串。我怀疑这与正则表达式范围 '.' 有关。其中包括除 '\n' 之外的所有内容。
不管怎样,我一直在玩“\R”之类的东西,但我就是找不到一个遵循我上面概述的两个规则的好的正则表达式模式。有什么帮助吗?
I need to split the following string and put each new line into a new array element.
this is line a.(EOL chars = '\r\n' or '\n')
(EOL chars)
this is line b.(EOL chars)
this is line c.(EOL chars)
this is the last line d.(OPTIONAL EOL chars)
(Note that the last line might not have any EOL characters present. The string also sometimes contains only 1 line, which is by definition the last one.)
The following rules must be followed:
- Empty lines (like the second line) should be discarded and not put
into the array. - EOL chars should not be included, because otherwise
my string comparisons fail.
So this should result in the following array:
[0] => "this is line a."
[1] => "this is line b."
[2] => "this is line c."
[3] => "this is the last line d."
I tried doing the following:
$matches = array();
preg_match_all('/^(.*)$/m', $str, $matches);
return $matches[1];
$matches[1] indeed contains each new line, but:
- Empty lines are included as well
- It seems that a '\r' character gets smuggled in anyway at the end of the strings in the array. I suspect this has something to do with the regex range '.' which includes everything except '\n'.
Anyway, I've been playing around with '\R' and whatnot, but I just can't find a good regex pattern that follows the two rules I outlined above. Any help please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需使用
preg_split()
来拆分正则表达式: ,如果有尾随换行符,您可能还需要
trim($string)
,这样您就不会得到额外的空数组元素。Just use
preg_split()
to split on the regular expression:Note, you might also want to
trim($string)
if there is a trailing newline, so you don't end up with an extra empty array element.有一个专门用于此目的的函数 -
file()
There is a function just for this -
file()
我认为
preg_split
将是go... 您可以使用适当的正则表达式来使用任何 EOL 字符作为分隔符。类似以下内容(正则表达式需要更详细一些):
希望有帮助,
I think
preg_split
would be the way to go... You can use an appropriate regexp to use any EOL character as separator.Something like the following (the regexp needs to be a bit more elaborate):
Hope that helps,
使用 preg_split 函数:
Use
preg_split
function: