tags:

views:

53

answers:

3

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_us::::Contact Us{/item2}

Is there a way?

+2  A: 

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

Stephen Doyle
+4  A: 
$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]);
RaYell
+3  A: 

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}
        )

)
Reinis I.