Eigen functie
- Code: Selecteer alles
<?php $fnc_text = 'return do_stuff($matches[0])'; // je functie tekst;
$txt = $_POST['tekst'];
$newtxt = preg_replace_callback('xxx', create_function('$matches', $fnc_text), $txt);
Met bestaande functie:
- Code: Selecteer alles
<?php $txt = $_POST['tekst'];
$newtxt = preg_replace_callback('xxx', 'functie_naam', $txt);
Meer informatie: http://www.php.net/preg_replace_callback en http://www.php.net/create_function.
Zelf gebruik ik dit in m'n CMS bijvoorbeeld om PHP content te parsen:
- Code: Selecteer alles
<?php $fnc_txt = 'if(!preg_match("#;[[:space:]]$#is", $matches[2])) { $matches[2] = $matches[2] . ";"; } ob_start(); eval($matches[2]); $c = ob_get_contents(); ob_end_clean(); return $c;';
$text = stripslashes($args['content']);
$text = utils::handle_text(preg_replace_callback('|<\?(php)?(.*?)\?>|is', create_function('$matches', $fnc_txt), $text));
echo $text;