php - Find matches and fill array -
im trying build filter function make easy editor users write html.
the users going input text this:
fusce eget sapien tortor hendrerit pharetra sed libero. vestibulum quis dui sed elit semper semper non molestie nulla. curabitur suscipit feugiat varius. {{accordion}} {{title}}1 first accordion title{{title_end}} {{body}}1 first accordion body{{body_end}} {{title}}1 second accordion title{{title_end}} {{body}}1 second accordion body{{body_end}} {{title}}1 third accordion title{{title_end}} {{body}}1 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 first accordion title{{title_end}} {{body}}2 first accordion body{{body_end}} {{title}}2 second accordion title{{title_end}} {{body}}2 second accordion body{{body_end}} {{title}}2 third accordion title{{title_end}} {{body}}2 third accordion body{{body_end}} {{accordion_end}} ut imperdiet odio quis diam ornare in congue purus rhoncus. quisque scelerisque est sed sapien facilisis facilisis turpis adipiscing. in hac habitasse platea dictumst.
note accordion blocks appears between other pieces of text, , appear in multiple occurences.
i using template theme accordions, accordions structured array, , inject themed accordions in right positions in text.
array ( [0] => array ( [title] => 1 first accordion title [body] => 1 first accordion body ) [1] => array ( [title] => 1 second accordion title [body] => 1 second accordion body ) [2] => array ( [title] => 1 third accordion title [body] => 1 third accordion body ) )
i have been messing around preg_replace_callback, cant find right way this.
so far ive been doing this:
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 $key => $value) { $data[$key]['title'] = $key . " title"; // need find titles $data[$key]['body'] = $key . " body"; // need find bodies } return theme("kunsten_accordion", array("data" => $data)); }
the theme() function passes array html template. named function lolfunction(), because had hoped have in 1 function format_accordion() :)
okay ended doing this:
function format_accordion($text) { $regex = '#{{accordion}}(.+?){{accordion_end}}#is'; return preg_replace_callback( $regex, function($content) { $title_regex = '#{{title}}(.+?){{title_end}}#is'; $body_regex = '#{{body}}(.+?){{body_end}}#is'; // find titles preg_match_all($title_regex, $content[1], $titles); // find bodies preg_match_all($body_regex, $content[1], $bodies); $data = array(); foreach($titles[1] $key => $title) { $data[$key]['title'] = $title; } foreach($bodies[1] $key => $body) { $data[$key]['body'] = $body; } return theme("kunsten_accordion", array("data" => $data)); }, $text); }
Comments
Post a Comment