J'avais fait (fait est très exagéré, c'est entièrement pompé sur le mod bbcode php de Fubonis) un truc du genre pour un système de commentaires :
function color($text)
{
$matches = array();
$match_count = preg_match_all("#\[php\](.*?)\[/php\]#si", $text, $matches);
for ( $i = 0; $i < $match_count; $i ++)
{
$before_replace = $matches[1][$i];
$after_replace = trim($matches[1][$i]);
$str_to_match = "[php]" . $before_replace . "[/php]";
$after_replace = str_replace('<', '<', $after_replace);
$after_replace = str_replace('>', '>', $after_replace);
$after_replace = str_replace('&', '&', $after_replace);
$added = FALSE;
if (preg_match('/^<\?.*?\?>$/si', $after_replace) <= 0)
{
$after_replace = "<?php $after_replace ?>";
$added = TRUE;
}
if(strcmp('4.2.0', phpversion()) > 0)
{
ob_start();
highlight_string($after_replace);
$after_replace = ob_get_contents();
ob_end_clean();
}
else
{
$after_replace = highlight_string($after_replace, TRUE);
}
if ($added == TRUE)
{
$after_replace = str_replace('<font color="#0000BB"><?php ', '<font color="#0000BB">', $after_replace);
$after_replace = str_replace('<font color="#0000BB">?></font>', '', $after_replace);
}
$after_replace = preg_replace('/<font color="(.*?)">/si', '<span style="color: \\1;">', $after_replace);
$after_replace = str_replace('</font>', '</span>', $after_replace);
$after_replace = str_replace("\n", '', $after_replace);
$replacement .= $after_replace;
$text = str_replace($str_to_match, $replacement, $text);
}
$new_match_count = preg_match_all("#\[blockquote\](.*?)\[/blockquote\]#si", $text, $new_matches);
for ( $i = 0; $i < $new_match_count; $i ++)
{
$ligne = explode("\n",$new_matches[1][$i]);
reset($ligne);
while($l = each($ligne))
{
ereg('^ *',$l[1],$tab);
$temp_text[$i] .= ereg_replace(' ',' ',$tab[0]);
$temp_text[$i] .= htmlentities(ereg_replace('^ *','',$l[1]));
$temp_text[$i] .= "<BR>";
}
$oc_to_replace[$i] = "[blockquote]".$new_matches[1][$i]."[/blockquote]";
$text = str_replace($oc_to_replace[$i],$temp_text[$i],$text);
}
return $text;
}
Utilisé avec le traitement suivant :
$comment = "[blockquote]".$comment."[/blockquote]";
$comment = str_replace('<?php','[/blockquote][php]',$comment);
$comment = str_replace('?>','[/php]fin_du_php_125485734[blockquote]',$comment);
$comment = str_replace('<?','[/blockquote][php]',$comment);
$comment = color(stripslashes($comment));
$comment = str_replace('fin_du_php_125485734',' <font color="#0000BB">?></font>',$comment);
C'est vraiment moche mais fonctionnel.
PS : Les balises php ici ont un souci avec les imbrications d'autres balises php, il faut neutraliser les balises contenues comme avec le bbcode code
