我正在尝试构建一个过滤器功能,以使编辑器用户可以轻松编写 html。
用户将输入如下文本:
Fusce eget sapien a tortor hendrerit pharetra sed a libero. Vestibulum quis dui sed elit semper semper non molestie nulla. Curabitur suscipit feugiat varius.
{{accordion}}
{{title}}1 This is the first accordion title{{title_end}}
{{body}}1 This is the first accordion body{{body_end}}
{{title}}1 This is the second accordion title{{title_end}}
{{body}}1 This is the second accordion body{{body_end}}
{{title}}1 This is the third accordion title{{title_end}}
{{body}}1 This is the third accordion body{{body_end}}
{{accordion_end}}
Nulla consequat cursus turpis vitae pretium. Suspendisse iaculis nisl rhoncus justo luctus vel scelerisque diam volutpat.
{{accordion}}
{{title}}2 This is the first accordion title{{title_end}}
{{body}}2 This is the first accordion body{{body_end}}
{{title}}2 This is the second accordion title{{title_end}}
{{body}}2 This is the second accordion body{{body_end}}
{{title}}2 This is the third accordion title{{title_end}}
{{body}}2 This is the third accordion body{{body_end}}
{{accordion_end}}
Ut imperdiet odio quis diam ornare in congue purus rhoncus. Quisque scelerisque est sed sapien facilisis a facilisis turpis adipiscing. In hac habitasse platea dictumst.
请注意,手风琴块出现在其他文本之间,并且它们多次出现。
我正在使用模板对手风琴进行主题化,因此我希望手风琴结构为数组,然后将主题手风琴注入文本中的正确位置。
Array
(
[0] => Array
(
[title] => 1 This is the first accordion title
[body] => 1 This is the first accordion body
)
[1] => Array
(
[title] => 1 This is the second accordion title
[body] => 1 This is the second accordion body
)
[2] => Array
(
[title] => 1 This is the third accordion title
[body] => 1 This is the third accordion body
)
)
我一直在搞乱 preg_replace_callback,但我真的找不到正确的方法来做到这一点。
到目前为止,我一直在这样做:
function format_accordion($text) {
$regex = '#{{accordion}}(.+?){{accordion_end}}#is';
return preg_replace_callback(
$regex,
"lolfunction",
$text);
}
function lolfunction($accordion_content) {
$title_regex = '#{{title}}(.+?){{title_end}}#is';
$body_regex = '#{{body}}(.+?){{body_end}}#is';
$data = array();
foreach ($accordion_content as $key => $value) {
$data[$key]['title'] = $key . " title"; // I need to find the titles
$data[$key]['body'] = $key . " body"; // I need to find the bodies
}
return theme("kunsten_accordion", array("data" => $data));
}
theme() 函数将数组传递给 html 模板。我将函数命名为 lolfunction(),因为我曾希望将所有内容都放在一个函数 format_accordion() 中 :)