开发者

php regex split string by [%%%]

Hi I need a preg_split regex that will split a string at substrings in square br开发者_运维问答ackets.

This example input:

$string = 'I have a string containing [substrings] in [brackets].';

should provide this array output:

[0]= 'I have a string containing '
[1]= '[substrings]'
[2]= ' in '
[3]= '[brackets]'
[4]= '.'


After reading your revised question:

This might be what you want:

$string = 'I have a string containing [substrings] in [brackets].';
preg_split('/(\[.*?\])/', $string, null, PREG_SPLIT_DELIM_CAPTURE);

You should get:

Array
(
    [0] => I have a string containing 
    [1] => [substrings]
    [2] =>  in 
    [3] => [brackets]
    [4] => .
)

Original answer:

preg_split('/%+/i', 'ot limited to 3 %%% so it can be %%%% or % or %%%%%, etc Tha');

You should get:

Array
(
    [0] => ot limited to 3 
    [1] =>  so it can be 
    [2] =>  or 
    [3] =>  or 
    [4] => , etc Tha
)

Or if you want a mimimum of 3 then try:

preg_split('/%%%+/i', 'Not limited to 3 %%% so it can be %%%% or % or %%%%%, etc Tha');

Have a go at http://regex.larsolavtorvik.com/


I think this is what you are looking for:

$array = preg_split('/(\[.*?\])/', $string, null, PREG_SPLIT_DELIM_CAPTURE);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜