简体   繁体   中英

PHP regular expression repetition problem

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}
';

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 
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM