If you want to write short codes, for example:
[[function_name_suffix parameter1 parameter2 ...]]
here's a more complete way, using preg_replace_callback and call_user_func_array to implement parameterized call_user_func_array .
function shortcodify($string){ return preg_replace_callback('#\[\[(.*?)\]\]#', function ($matches) { $whitespace_explode = explode(" ", $matches[1]); $fnName = 'shortcode_'.array_shift($whitespace_explode); return function_exists($fnName) ? call_user_func_array($fnName,$whitespace_explode) : $matches[0]; }, $string); }
If this function is defined:
function shortcode_name($firstname="",$lastname=""){ return "<span class='firstname'>".$firstname."</span> <span class='lastname'>".$lastname."</span>"; }
Then this call
print shortcodify("My name is [[name armel larcier]]");
It will display:
My name is <span class='firstname'>armel</span> <span class='lastname'>larcier</span>
This is just what I implemented right now based on the idea of supertrue.
Any feedback is more than welcome.
Armel larcier
source share