preg_split如何在结果中包含拆分分隔符?

时间:2021-12-26 03:03:28

I have this simple pattern that splits a text into periods

我有这个简单的模式,将文本分成句点

$text = preg_split("/[\.:!\?]+/", $text);

but i want to include . : or ! at the end of array items

但我想包括。 : 要么 !在数组项的末尾

IE now for "good:news.everyone!" i have:

IE现在用于“好:新闻!每个人!”我有:

array("good","news","everyone","");

but what i want is:

但我想要的是:

array("good:","news.","everyone!","");

2 个解决方案

#1


43  

Here you go:

干得好:

preg_split('/([^.:!?]+[.:!?]+)/', 'good:news.everyone!', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

How it works: The pattern actually turns everything into a delimiter. Then, to include these delimiters in the array, you can use the PREG_SPLIT_DELIM_CAPTURE constant. This will return an array like:

工作原理:模式实际上将所有内容都变成了分隔符。然后,要在数组中包含这些分隔符,可以使用PREG_SPLIT_DELIM_CAPTURE常量。这将返回一个数组,如:

array (
    0 => '',
    1 => 'good:',
    2 => '',
    3 => 'news.',
    4 => '',
    5 => 'everyone!',
    6 => '',
);

To get rid of the empty values, use PREG_SPLIT_NO_EMPTY. To combine two or more of these constants, we use the bitwise | operator. The result:

要删除空值,请使用PREG_SPLIT_NO_EMPTY。要组合这些常量中的两个或更多个,我们使用按位|运营商。结果:

array (
    0 => 'good:',
    1 => 'news.',
    2 => 'everyone!'
);

#2


4  

No use for PREG_SPLIT_DELIM_CAPTURE if you use a positive lookbehind in your pattern. The function will keep the delimiters.

如果在模式中使用正向lookbehind,则不能使用PREG_SPLIT_DELIM_CAPTURE。该函数将保留分隔符。

$text = preg_split('/(?<=[.:!?])/', 'good:news.everyone!', 0, PREG_SPLIT_NO_EMPTY);

If you use lookbehind, it will just look for the character without matching it. So, in the case of preg_split(), the function will not discard the character.

如果你使用lookbehind,它只会寻找不匹配它的角色。因此,在preg_split()的情况下,该函数不会丢弃该字符。

The result without PREG_SPLIT_NO_EMPTY flag:

没有PREG_SPLIT_NO_EMPTY标志的结果:

array (
    0 => 'good:',
    1 => 'news.',
    2 => 'everyone!',
    3 => ''
);

The result with PREG_SPLIT_NO_EMPTY flag:

PREG_SPLIT_NO_EMPTY标志的结果:

array (
    0 => 'good:',
    1 => 'news.',
    2 => 'everyone!'
);

You can test it using this PHP Online Function Tester.

您可以使用此PHP在线函数测试程序对其进行测试。

#1


43  

Here you go:

干得好:

preg_split('/([^.:!?]+[.:!?]+)/', 'good:news.everyone!', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

How it works: The pattern actually turns everything into a delimiter. Then, to include these delimiters in the array, you can use the PREG_SPLIT_DELIM_CAPTURE constant. This will return an array like:

工作原理:模式实际上将所有内容都变成了分隔符。然后,要在数组中包含这些分隔符,可以使用PREG_SPLIT_DELIM_CAPTURE常量。这将返回一个数组,如:

array (
    0 => '',
    1 => 'good:',
    2 => '',
    3 => 'news.',
    4 => '',
    5 => 'everyone!',
    6 => '',
);

To get rid of the empty values, use PREG_SPLIT_NO_EMPTY. To combine two or more of these constants, we use the bitwise | operator. The result:

要删除空值,请使用PREG_SPLIT_NO_EMPTY。要组合这些常量中的两个或更多个,我们使用按位|运营商。结果:

array (
    0 => 'good:',
    1 => 'news.',
    2 => 'everyone!'
);

#2


4  

No use for PREG_SPLIT_DELIM_CAPTURE if you use a positive lookbehind in your pattern. The function will keep the delimiters.

如果在模式中使用正向lookbehind,则不能使用PREG_SPLIT_DELIM_CAPTURE。该函数将保留分隔符。

$text = preg_split('/(?<=[.:!?])/', 'good:news.everyone!', 0, PREG_SPLIT_NO_EMPTY);

If you use lookbehind, it will just look for the character without matching it. So, in the case of preg_split(), the function will not discard the character.

如果你使用lookbehind,它只会寻找不匹配它的角色。因此,在preg_split()的情况下,该函数不会丢弃该字符。

The result without PREG_SPLIT_NO_EMPTY flag:

没有PREG_SPLIT_NO_EMPTY标志的结果:

array (
    0 => 'good:',
    1 => 'news.',
    2 => 'everyone!',
    3 => ''
);

The result with PREG_SPLIT_NO_EMPTY flag:

PREG_SPLIT_NO_EMPTY标志的结果:

array (
    0 => 'good:',
    1 => 'news.',
    2 => 'everyone!'
);

You can test it using this PHP Online Function Tester.

您可以使用此PHP在线函数测试程序对其进行测试。