PHP: <?php function handle_bbcode_php($code){ if (!is_array($codefind1)) { $codefind1 = array( '<br>', // <br> to nothing '<br />' // <br /> to nothing ); $codereplace1 = array( '', '' ); $codefind2 = array( '>', // > to > '<', // < to < '"', // " to ", '&', // & to & '[', // [ to [ ']', // ] to ] ); $codereplace2 = array( '>', '<', '"', '&', '[', ']', ); } // remove htmlspecialchars'd bits and excess spacing $code = trim(str_replace($codefind1, $codereplace1, $code)); $code = str_replace($codefind2, $codereplace2, $code); // finish replacements // do we have an opening <? tag? if (!preg_match('#<\?#si', $code)) { // if not, replace leading newlines and stuff in a <?php tag and a closing tag at the end $code = "<?php BEGIN__VBULLETIN__CODE__SNIPPET $code \r\nEND__VBULLETIN__CODE__SNIPPET ?>"; $addedtags = true; } else { $addedtags = false; } // highlight the string $oldlevel = error_reporting(0); if (PHP_VERSION >= '4.2.0') { $code = highlight_string($code, true); } else { @ob_start(); highlight_string($code); $code = @ob_get_contents(); @ob_end_clean(); } error_reporting($oldlevel); // if we added tags above, now get rid of them from the resulting string if ($addedtags) { $search = array( '#<\?php( | )BEGIN__VBULLETIN__CODE__SNIPPET( | )#siU', '#(<(span|font)[^>]*>)<\?(</\\2>(<\\2[^>]>))php( | )BEGIN__VBULLETIN__CODE__SNIPPET( | )#siU', '#END__VBULLETIN__CODE__SNIPPET( | )\?(>|>)#siU' ); $replace = array( '', '\\4', '' ); $code = preg_replace($search, $replace, $code); } $code = preg_replace('/&#([0-9]+);/', '&#$1;', $code); // allow unicode entities back through $code = str_replace(array('[', ']'), array('[', ']'), $code); return $code; } $code ="<?php $test= str_replace(array('[', ']'), array('[', ']'), $code);?>"; echo handle_bbcode_php($code); ?>