Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

52 users online



Implementing [PHP] Tags

Implementing [PHP] Tags

Currently viewing this thread: 1 (0 members and 1 guests)


Page 2 out of 3
Recessive

Recessive

Status: Offline!
Quote:

Originally posted by CDude
test it first recessive

Erm, its been tested?

Recessive

Recessive

Status: Offline!

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. Smile

CDude

CDude

nothing
Status: Offline!

works now

___________________

http://celerondude.com

Recessive

Recessive

Status: Offline!
Code:

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.

Adam

Adam

Status: Offline!

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

Darker

Darker

Status: Offline!

*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 Grin

Code:

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

Recessive

Recessive

Status: Offline!
Quote:

Originally posted by Darker

Code:


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 Grin

Also, that doesn't make use of the highlight_string() functions alternative return type, but uses output buffering to capture the string.

Darker

Darker

Status: Offline!

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

tbi

tbi

Status: Offline!

i looked into the highlight function from phpBB board , i can olny say Shocked .

___________________

sillerdesign|de

php_brian

php_brian

Status: Offline!

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:

Code:


{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)

Code:


$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

Page 2 out of 3
Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0193 seconds.