我有以下字符串:
{slider id="1"} {slide title="title1"} {content} content1{/content} {caption}caption1 {/caption} {/slide} {slide title="title2"} {content} content2{/content} {caption} caption2 {/caption} {/slide} {/slider}
我想通过正则表达式解析它,但我被卡住了。
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);
结果是:
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
)
)
我认为问题出在这里:{/ slide} \ s *)* {/ slider}第二个*是哪里,但我不知道需要使用哪些功能来获得正确的结果。
我想在数组中得到这个结果:
1
title1
content1
caption1
2
title2
content2
caption2
UPDATE1: 正则表达式必须与字符串中的多个滑块实例一起使用,如:
$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}
';
答案 0 :(得分:1)
这是怎么回事?
根据OP的意见更新了答案:
$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>';
以上产生以下输出:
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
)