PHP REGEX - 通过 preg_split 在换行符处将文本转换为数组

发布于 2024-08-30 12:07:17 字数 992 浏览 12 评论 0原文

已编辑:

需要有关拆分数组

数组示例的帮助:

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

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

发布评论

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

评论(5

零崎曲识 2024-09-06 12:07:17

“换行符”定义不明确。 Windows 仅使用 CR+LF (\r\n)、Linux LF (\n)、OSX CR (\r)。

preg_* 常规异常中有一个鲜为人知的特殊字符 \R 与所有三个匹配:

preg_match('/^\R$/', "\r\n"); // 1

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

preg_match('/^\R$/', "\r\n"); // 1
忆悲凉 2024-09-06 12:07:17

这是一个有效的示例,即使您在字符串中嵌入了冒号字符(但不在行首):

$input = ":some normal text
:some long text here, and so on... sometimes
i'm breaking: down and...
:some normal text
:some normal text";

$array = preg_split('/$\R?^:/m', $input);
print_r($array);

结果:

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
)

Here's an example that works, even if you have a colon character embedded inside the string (but not at start of the line):

$input = ":some normal text
:some long text here, and so on... sometimes
i'm breaking: down and...
:some normal text
:some normal text";

$array = preg_split('/$\R?^:/m', $input);
print_r($array);

result:

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
)
成熟的代价 2024-09-06 12:07:17

file() 将文件读入一个数组。

file() reads a file into an array.

笑红尘 2024-09-06 12:07:17

如果您在 : 字符上拆分数组..

print_r(preg_split('/:/', $input));

If you split the array on the : character..

print_r(preg_split('/:/', $input));
永不分离 2024-09-06 12:07:17
$lines = explode("\n", $text);
$lines = explode("\n", $text);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文