How do I include the split delimiter in results for preg_split()?

落花浮王杯 提交于 2019-12-28 00:20:12

问题


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 the array items.

That is, now for "good:news.everyone!" I have:

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

But I want:

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

回答1:


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:

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:

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



回答2:


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

$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.

The result without PREG_SPLIT_NO_EMPTY flag:

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

The result with PREG_SPLIT_NO_EMPTY flag:

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

You can test it using this PHP Online Function Tester.



来源:https://stackoverflow.com/questions/11758465/how-do-i-include-the-split-delimiter-in-results-for-preg-split

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!