嗨我需要一个preg_split
正则表达式,它会在方括号中的子串处拆分一个字符串。
此示例输入:
$string = 'I have a string containing [substrings] in [brackets].';
应提供此数组输出:
[0]= 'I have a string containing '
[1]= '[substrings]'
[2]= ' in '
[3]= '[brackets]'
[4]= '.'
答案 0 :(得分:7)
阅读修改后的问题:
这可能是你想要的:
$string = 'I have a string containing [substrings] in [brackets].';
preg_split('/(\[.*?\])/', $string, null, PREG_SPLIT_DELIM_CAPTURE);
你应该得到:
Array
(
[0] => I have a string containing
[1] => [substrings]
[2] => in
[3] => [brackets]
[4] => .
)
原始回答:
preg_split('/%+/i', 'ot limited to 3 %%% so it can be %%%% or % or %%%%%, etc Tha');
你应该得到:
Array
(
[0] => ot limited to 3
[1] => so it can be
[2] => or
[3] => or
[4] => , etc Tha
)
或者如果您想要最小值3,请尝试:
preg_split('/%%%+/i', 'Not limited to 3 %%% so it can be %%%% or % or %%%%%, etc Tha');
答案 1 :(得分:2)
我认为这就是你要找的东西:
$array = preg_split('/(\[.*?\])/', $string, null, PREG_SPLIT_DELIM_CAPTURE);