开发者

Splitting a string in certain way

I have the following string

{item1}home::::Home{/item1}{item2}contact_us::::Contact Us{/item2}{item3}.....

and so it goes on.

I need to split the string the following way

1=>{item1}home::::Home{/item1}

2=>{item2}contact开发者_StackOverflow_us::::Contact Us{/item2}

Is there a way?


$input = '{item1}home::::Home{/item1}{item2}contact_us::::Contact Us{/item2}{item3}.....';
$regex = '/{(\w+)}.*{\/\1}/';
preg_match_all($regex, $input, $matches);
print_r($matches[0]);


You could do it like this:

$text = "{item1}home::::Home{/item1}{item2}contact_us::::Contact Us{/item2}{item3}.....){/item3}";
preg_match_all('/{item\d}.+?{\/item\d}/', $text, $results);

var_dump($results) would produce:

Array
(
    [0] => Array
        (
            [0] => {item1}home::::Home{/item1}
            [1] => {item2}contact_us::::Contact Us{/item2}
            [2] => {item3}.....){/item3}
        )

)


Use preg_split() with the regex pattern /{.*?}.*?{\/.*?}/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜