1
我試圖建立一個過濾器函數,使編輯器用戶可以輕鬆地編寫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));
}
主題()函數傳遞數組到HTML模板。我命名的函數lolfunction(),因爲我曾希望在短短的一個功能format_accordion():)一切
才能真正爲你的用戶更容易當他們可以使用標準XML(甚至是HTML)來標記文本時,寫{{accordion}} {{title}} {{body}}?如果您使用XML(' '),則不需要編寫自定義分析器。 –
2013-05-13 20:15:26
我不想讓他們寫html,這意味着他們必須寫類,也就是說。但是,讓他們編寫xml也許是個好主意。但是,我仍然必須用html +類替換xml標記。有任何想法嗎? – tolborg 2013-05-13 20:22:06
除了它的其他方式。我有一個字符串。然後將匹配轉換爲數組。然後將數組格式化爲新的字符串。然後將新字符串注入原始文本。 – tolborg 2013-05-13 20:27:12