字符串到数组,用新行和括号分隔
String to array, split by new lines and brackets
我有一个很大的字符串块,我需要根据它们是用方括号括起来还是用换行符分隔成数组。
输入:
[this is block
this is also same "block"
this is same block
another same block]
new block!
another new block!
[this is new block
this is also a new block]
我尝试过的许多事情之一:
$block_lines = preg_split('/\[([^]]+)\]|\r/', $block_content);
预期结果:
Array
(
[0] => 'this is block
this is also same "block"
this is same block
another same block'
[1] => 'new block!'
[2] => 'another new block!'
[3] => 'this is new block
this is also a new block'
)
实际结果:
Array
(
[0] => 'new block!'
[1] => 'another new block!'
[2] => ''
)
首先匹配所有方括号匹配项(可能包含新行),否则匹配单行。
在这种情况下,我更喜欢 preg_match_all()
而不是 preg_split()
的原因是,简单来说,您实际上并不是要执行动态爆炸,而是要找到匹配项。
代码:(Demo)
$text = '[this is block
this is also same "block"
this is same block
another same block]
new block!
another new block!
[this is new block
this is also a new block]';
var_export(preg_match_all('~\[[^\]]*]|.+~', $text, $matches) ? $matches[0] : 'nothing');
输出:
array (
0 => '[this is block
this is also same "block"
this is same block
another same block]',
1 => 'new block!',
2 => 'another new block!',
3 => '[this is new block
this is also a new block]',
)
您可以在 preg_split
:
中使用此正则表达式
/\[([^]]+)]|\R/
它将字符串拆分为 [
和 ]
内的字符串或换行符。通过使用 PREG_SPLIT_DELIM_CAPTURE
标志,我们也可以捕获 []
的内容:
$string = '[this is block
this is also same "block"
this is same block
another same block]
new block!
another new block!
[this is new block
this is also a new block]';
print_r(preg_split('/\[([^]]+)]|\R/', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE));
输出:
Array (
[0] => this is block
this is also same "block"
this is same block
another same block
[1] => new block!
[2] => another new block!
[3] => this is new block
this is also a new block
)
或者,要更改逻辑的措辞,您希望在所有不在方括号表达式内的换行符上展开。您可以使用 (*SKIP)(*FAIL)
来匹配和忽略括号中的表达式,并在所有通过过滤器的换行符上爆炸。
代码:(Demo)
var_export(preg_split('~\[[^\]]*](*SKIP)(*FAIL)|\R~', $text));
输出:
array (
0 => '[this is block
this is also same "block"
this is same block
another same block]',
1 => 'new block!',
2 => 'another new block!',
3 => '[this is new block
this is also a new block]',
)
我有一个很大的字符串块,我需要根据它们是用方括号括起来还是用换行符分隔成数组。
输入:
[this is block
this is also same "block"
this is same block
another same block]
new block!
another new block!
[this is new block
this is also a new block]
我尝试过的许多事情之一:
$block_lines = preg_split('/\[([^]]+)\]|\r/', $block_content);
预期结果:
Array
(
[0] => 'this is block
this is also same "block"
this is same block
another same block'
[1] => 'new block!'
[2] => 'another new block!'
[3] => 'this is new block
this is also a new block'
)
实际结果:
Array
(
[0] => 'new block!'
[1] => 'another new block!'
[2] => ''
)
首先匹配所有方括号匹配项(可能包含新行),否则匹配单行。
在这种情况下,我更喜欢 preg_match_all()
而不是 preg_split()
的原因是,简单来说,您实际上并不是要执行动态爆炸,而是要找到匹配项。
代码:(Demo)
$text = '[this is block
this is also same "block"
this is same block
another same block]
new block!
another new block!
[this is new block
this is also a new block]';
var_export(preg_match_all('~\[[^\]]*]|.+~', $text, $matches) ? $matches[0] : 'nothing');
输出:
array (
0 => '[this is block
this is also same "block"
this is same block
another same block]',
1 => 'new block!',
2 => 'another new block!',
3 => '[this is new block
this is also a new block]',
)
您可以在 preg_split
:
/\[([^]]+)]|\R/
它将字符串拆分为 [
和 ]
内的字符串或换行符。通过使用 PREG_SPLIT_DELIM_CAPTURE
标志,我们也可以捕获 []
的内容:
$string = '[this is block
this is also same "block"
this is same block
another same block]
new block!
another new block!
[this is new block
this is also a new block]';
print_r(preg_split('/\[([^]]+)]|\R/', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE));
输出:
Array (
[0] => this is block
this is also same "block"
this is same block
another same block
[1] => new block!
[2] => another new block!
[3] => this is new block
this is also a new block
)
或者,要更改逻辑的措辞,您希望在所有不在方括号表达式内的换行符上展开。您可以使用 (*SKIP)(*FAIL)
来匹配和忽略括号中的表达式,并在所有通过过滤器的换行符上爆炸。
代码:(Demo)
var_export(preg_split('~\[[^\]]*](*SKIP)(*FAIL)|\R~', $text));
输出:
array (
0 => '[this is block
this is also same "block"
this is same block
another same block]',
1 => 'new block!',
2 => 'another new block!',
3 => '[this is new block
this is also a new block]',
)