<?php
$text = "Testing text splitting\nWith a newline!";
$textArray = preg_split('/\s+/', $text, 0, PREG_SPLIT_DELIM_CAPTURE);
print_r($textArray);
上面的代码将输出以下内容:空白分隔符分割
Array
(
[0] => Testing
[1] => text
[2] => splitting
[3] => With
[4] => a
[5] => newline!
)
但是据我所知,PREG_SPLIT_DELIM_CAPTURE标志应被捕获空格分隔符在数组中。我错过了什么吗?
编辑:好的,在重读了我现在理解的文档后,PREG_SPLIT_DELIM_CAPTURE并不适用于这种情况。我需要的输出会是这样的:
Array
(
[0] => Testing
[1] => ' '
[2] => text
[3] => ' '
[4] => splitting
[5] => '\n'
[6] => With
[7] => ' '
[8] => a
[9] => ' '
[10] => newline!
)
谁告诉你的? –
我的意思是如果情况并非如此,那么还有不同的方法吗? – user2827048
根据php [manual](http://php.net/manual/en/function.preg-split.php),'PREG_SPLIT_DELIM_CAPTURE'会导致模式的任何加括号的部分也被返回。你想要的输出是什么? –