php regex split string by [%%%]

前端 未结 2 1217
春和景丽
春和景丽 2021-01-21 07:17

Hi I need a preg_split regex that will split a string at substrings in square brackets.

This example input:

$string = \'I have a string cont         


        
2条回答
  •  Happy的楠姐
    2021-01-21 07:49

    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/

提交回复
热议问题