0

I have the following string:

{slider id="1"} {slide title="title1"} {content} content1{/content} {caption}caption1 {/caption} {/slide} {slide title="title2"} {content} content2{/content} {caption} caption2 {/caption} {/slide} {/slider}

I would like to parse it by regular expression, but I'm stuck.

preg_match_all('/{slider\s+id="([0-9]+)"\s*}(\s*{slide\s*title="(.*?)"\s*}\s*{content}(.*?){\/content}\s*{caption}(.*?){\/caption}\s*{\/slide}\s*)*{\/slider}/sm', $body, $sliders);

The result is:

Array
(
    [0] => Array
        (
            [0] => {slider id="1"} {slide title="title1"} {content} content1{/content} {caption}caption1 {/caption} {/slide} {slide title="title2"} {content} content2{/content} {caption} caption2 {/caption} {/slide} {/slider}
        )

    [1] => Array
        (
            [0] => 1
        )

    [2] => Array
        (
            [0] => {slide title="title2"} {content} content2{/content} {caption} caption2 {/caption} {/slide} 
        )

    [3] => Array
        (
            [0] => title2
        )

    [4] => Array
        (
            [0] =>  content2
        )

    [5] => Array
        (
            [0] =>  caption2 
        )

)

I think the problem is around here: {/slide}\s*)*{/slider} Where the second * is, but I don't know what features need to use to get the right result.

I would like to get this result in the array:

1
title1
content1
caption1
2
title2
content2
caption2

UPDATE1: The regular expression must work with multiple slider instances in the string, like:

$body = '
    {slider id="1"} {slide title="title1"} {content} content1{/content} {caption}caption1 {/caption} {/slide} {slide title="title2"} {content} content2{/content} {caption} caption2 {/caption} {/slide} {/slider}
    {slider id="2"} {slide title="title1"} {content} content1{/content} {caption}caption1 {/caption} {/slide} {slide title="title2"} {content} content2{/content} {caption} caption2 {/caption} {/slide} {/slider}
    {slider id="3"} {slide title="title1"} {content} content1{/content} {caption}caption1 {/caption} {/slide} {slide title="title2"} {content} content2{/content} {caption} caption2 {/caption} {/slide} {/slider}
';

1 Answer 1

1

How is this?

Updated answer follows, in light of OP's comments:

$body = '
{slider id="1"} {slide title="title1"} {content} content1{/content} {caption}caption1 {/caption} {/slide} {slide title="title2"} {content} content2{/content} {caption} caption2 {/caption} {/slide} {/slider}
{slider id="2"} {slide title="title1"} {content} content1{/content} {caption}caption1 {/caption} {/slide} {slide title="title2"} {content} content2{/content} {caption} caption2 {/caption} {/slide} {/slider}
{slider id="3"} {slide title="title1"} {content} content1{/content} {caption}caption1 {/caption} {/slide} {slide title="title2"} {content} content2{/content} {caption} caption2 {/caption} {/slide} {/slider}
';

preg_match_all('/{slide\s+title="(.*?)"\s*}\s*{content}(.*?){\/content}\s*{caption}(.*?){\/caption}\s*{\/slide}\s*/sm', $body, $sliders, PREG_SET_ORDER);

$new_array = array();

for ($i = 0; $i < count($sliders); $i++) {
    for ($j = 1; $j < count($sliders[$i]); $j++) {
        $new_array[] = $sliders[$i][$j];
    }
}

echo '<pre>'.print_r($new_array,1).'</pre>';

The above produces the following output:

Array
(
[0] => title1
[1] =>  content1
[2] => caption1 
[3] => title2
[4] =>  content2
[5] =>  caption2 
[6] => title1
[7] =>  content1
[8] => caption1 
[9] => title2
[10] =>  content2
[11] =>  caption2 
[12] => title1
[13] =>  content1
[14] => caption1 
[15] => title2
[16] =>  content2
[17] =>  caption2 
)
Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, this gives nice result, but it doesn't care if there are multiple {slider} instances in the code. I will add now a little modification for the question where this code failes.
I've just tried your updated extract and I get a long 1-D array of the elements from all sliders. Is this not the desired behaviour?
Maybe I wasn't clear enough. The result is good with your array, but I needed that values to get it from the regular expression with the slider id too. question updated again :)
Ahh, I think I see what you mean... you need a MD array with the slider ID as the key to another array?
Array type isn't matter for me, just the result of the regular expression. It don't have to be exactly the same result what I wrote in the question, but I think a regular expression can find each of that strings and can be in some way in the $sliders array. Currently it doesn't work, because it have to parse {slide}{/slide} multiple time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.