|
Implementing [PHP] Tags
Implementing [PHP] Tags
Currently viewing this thread: 1 (0 members and 1 guests)
 November 13th, 2002 08:01 AM
Originally posted by CDude
test it first recessive
Erm, its been tested?
 November 13th, 2002 08:09 AM
Ok, so the one I posted doesn't work if you do not include <?php ?> tags.
It was just something I knocked up, quickly tested and posted. I'm sure someone could implement something that would check for them, and if not include them. 
 November 13th, 2002 09:07 AM
works now
___________________
http://celerondude.com
 November 13th, 2002 09:51 AM
echo preg_replace("/(?:\[php\])[\r\n]*(.*)(?:\[\/php\])/esi", "parsePHP('\\1')", $string);
function parsePHP ( $php )
{
return ("<?" == substr( $php, 0, 2)) ? highlight_string( $php, TRUE )
: highlight_string("<?php ". $php ." ?>", TRUE );
}
My above 'bug' fixed.
 November 13th, 2002 03:12 PM
well, i'll proably end up using regex, but I though using str posisions was so cool!
___________________
Current Project: LivermoreMuscle.com
Sig Hosted by Motorspin
 November 13th, 2002 04:35 PM

*adds my input*
NOTE: I'm NOT claiming this to be my code, while i DO understand it and know how it works, i did NOT write it myself, not this time around anyways... open source is handy 
function bbcode($str){
$str = preg_replace("/(\[)(php)(])(\r\n)*(.*)(\[\/php\])/esiU", "phphighlite('\\5')", $str);
return $str;
}
// Start phphighlite
function phphighlite($code) {
//PHP 4 only
if (floor(phpversion())<4) {
$buffer=$code;
} else {
$code = str_replace("<br>", "", $code);
$code = str_replace("<br />", "", $code);
$code = str_replace(">", ">", $code);
$code = str_replace("<", "<", $code);
$code = str_replace("&", "&", $code);
$code = str_replace('$', '\$', $code);
$code = str_replace('\n', '\\\\n', $code);
$code = str_replace('\r', '\\\\r', $code);
$code = str_replace('\t', '\\\\t', $code);
$code = stripslashes($code);
if (!strpos($code,"<?") and substr($code,0,2)!="<?") {
$code="<?".trim($code)."?>";
$addedtags=1;
}
ob_start();
$oldlevel=error_reporting(0);
highlight_string($code);
error_reporting($oldlevel);
$buffer = ob_get_contents();
ob_end_clean();
if ($addedtags) {
$openingpos = strpos($buffer,'<?');
$closingpos = strrpos($buffer, '?');
$buffer=substr($buffer, 0, $openingpos).substr($buffer, $openingpos+5, $closingpos-($openingpos+5)).substr($buffer, $closingpos+5);
}
$buffer = str_replace(""", "\"", $buffer);
}
return "$buffer";
}
the returned value of the phphighlight function is just the text highlighted... you can format it how you like. e.g.
php:<hr>$buffer<hr>
hope this helps someone
 November 13th, 2002 06:47 PM

Originally posted by Darker
function bbcode($str){
$str = preg_replace("/(\[)(php)(])(\r\n)*(.*)(\[\/php\])/esiU", "phphighlite('\\5')", $str);
return $str;
}
// Start phphighlite
function phphighlite($code) {
//PHP 4 only
if (floor(phpversion())<4) {
$buffer=$code;
} else {
$code = str_replace("<br>", "", $code);
$code = str_replace("<br />", "", $code);
$code = str_replace(">", ">", $code);
$code = str_replace("<", "<", $code);
$code = str_replace("&", "&", $code);
$code = str_replace('$', '\$', $code);
$code = str_replace('\n', '\\\\n', $code);
$code = str_replace('\r', '\\\\r', $code);
$code = str_replace('\t', '\\\\t', $code);
$code = stripslashes($code);
if (!strpos($code,"<?") and substr($code,0,2)!="<?") {
$code="<?".trim($code)."?>";
$addedtags=1;
}
ob_start();
$oldlevel=error_reporting(0);
highlight_string($code);
error_reporting($oldlevel);
$buffer = ob_get_contents();
ob_end_clean();
if ($addedtags) {
$openingpos = strpos($buffer,'<?');
$closingpos = strrpos($buffer, '?');
$buffer=substr($buffer, 0, $openingpos).substr($buffer, $openingpos+5, $closingpos-($openingpos+5)).substr($buffer, $closingpos+5);
}
$buffer = str_replace(""", "\"", $buffer);
}
return "$buffer";
}
vB uses very unefficient regex's 
Also, that doesn't make use of the highlight_string() functions alternative return type, but uses output buffering to capture the string.
 November 13th, 2002 07:10 PM
yeh, i have to say i wouldn't write the regex like that, but then i can't remember much regex offhand *sigh* as for ob, i would use that as opposed to the return true option too... just preference i guess
 November 13th, 2002 07:30 PM
i looked into the highlight function from phpBB board , i can olny say .
___________________
sillerdesign|de
 January 4th, 2003 07:38 AM
Well, I've been intrested in this issue and came across an error with Recessive's regular expression. For some reason when I did this:
{PHP]
<?php
print 'hi';
?>
{/PHP]
{PHP]
<?php
$str = 'hi';
?>
{/PHP]
(the { are [)
It would ignore the end of the first php block and the start of the second. So it would display what's between the start of the first and the end of the second. So here's what I did to get it fixed: (this is off of Recessive's first example, but can be easily transformed to his 'bug' fix)
$str = preg_replace("/(\[PHP\])[\r\n]*(.*?)(\[\/PHP\])/esi", "highlight_string('\\2',true)", $str);
___________________
Why can't I put the recycle bin in the recycle bin?
brosner.com | coderforums.com
|