PHP中使用正则表达式将多行字符串转换为多元素数组

发布于 2024-12-26 11:24:31 字数 874 浏览 2 评论 0原文

我需要拆分以下字符串并将每个新行放入新的数组元素中。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兔姬 2025-01-02 11:24:31

只需使用 preg_split() 来拆分正则表达式

// Split on \n, \r is optional..
// The last element won't need an EOL.
$array = preg_split("/\r?\n/", $string);

: ,如果有尾随换行符,您可能还需要 trim($string) ,这样您就不会得到额外的空数组元素。

Just use preg_split() to split on the regular expression:

// Split on \n, \r is optional..
// The last element won't need an EOL.
$array = preg_split("/\r?\n/", $string);

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.

简单 2025-01-02 11:24:31

有一个专门用于此目的的函数 - file()

There is a function just for this - file()

七月上 2025-01-02 11:24:31

我认为 preg_split 将是go... 您可以使用适当的正则表达式来使用任何 EOL 字符作为分隔符。

类似以下内容(正则表达式需要更详细一些):

$array = preg_split('/[\n\r]+/', $string);

希望有帮助,

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):

$array = preg_split('/[\n\r]+/', $string);

Hope that helps,

等待我真够勒 2025-01-02 11:24:31

使用 preg_split 函数:

$array = preg_split('/[\r\n]+/', $string);

Use preg_split function:

$array = preg_split('/[\r\n]+/', $string);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文