我正在解析一个文本,但是当缺少空格时我无法获得一个片段(这没关系)
编辑:我在自由文本中添加了冒号。
编辑:嗯,这是可以写入键值对的任意文本格式。丢弃元素[0],数组中的其余元素会产生一系列键值。它接受多行值。
这是测试用例文本:
:part1 only one \s removed:OK
:part2 :text :with
new lines
on it
:noSpaceAfterThis
:thisShoudBeAStandAlongText but: here there are more text
:part4 :even more text
这就是我要的:
Array
(
[0] =>
[1] => part1
[2] => only one \s removed:OK
[3] => part2
[4] => :text :with
new lines
on it
[5] => noSpaceAfterThis
[6] =>
[7] => thisShoudBeAStandAlongText
[8] => but: here there are more text
[9] => part4
[10] => :even more text
)
这就是我得到的:
Array
(
[0] =>
[1] => part1
[2] => only one \s removed:OK
[3] => part2
[4] => :text :with
new lines
on it
[5] => noSpaceAfterThis
[6] => :thisShoudBeAStandAlongText but: here there are more text
[7] => part4
[8] => :even more text
)
这是我的测试代码:
<?php
$text = '
:part1 only one \s removed:OK
:part2 :text :with
new lines
on it
:noSpaceAfterThis
:thisShoudBeAStandAlongText but: here there are more text
:part4 :even more text';
echo '<pre>';
// my effort so far:
$ret = preg_split('|\r?\n:([\w\d]+)(?:\r?\s)?|i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($ret);
// nor this one:
$ret = preg_split('|\r?\n:([\w\d]+)\r?\s?|i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($ret);
// for debuging, an extra capturing group
$ret = preg_split('|\r?\n:([\w\d]+)(\r?\s)?|i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
var_dump($ret);