
November 13th, 2002
01:15 AM
Implementing [PHP] Tags
I was able to do is using string posistions, suprisied myself there. But how can I do it with regular expression? I have regex that looks like:
$Regex = '/(\[ PHP \])(.+)(\[\/ PHP \])/'; //spaces add to prevent vB parse
//replace with
$Output = preg_replace($Regex, hightlight_string("<?php".2."?>");
I think the string postions is impressive but I't can be used as easily as regex, and ideas or links?
//string position code for the wonderors
$Text = "here is some code [ PHP ]print \$Var;[/ PHP ] look!";
$End = strpos($Text, "[/ PHP ]");
$Start = strpos($Text, "[ PHP ]");
$Length = ($End) - ($Start + 5);
$Code = substr($Text, $Start+5, $Length);
highlight_string("<?php ".$Code." ?>");
___________________
Current Project: LivermoreMuscle.com
Sig Hosted by Motorspin

November 13th, 2002
03:46 AM
That's an awsome idea..unfortuantly..im no help ::classic:
___________________
<?php
$var="Working on my sig :-P";
echo =$var;
?>

November 13th, 2002
03:47 AM
whats an awful idea? Coming from the guy who can't even do the php right in his sig.
___________________
Current Project: LivermoreMuscle.com
Sig Hosted by Motorspin

November 13th, 2002
06:03 AM
Perhaps you should read people's posts more carefully before you reply, Adman.
[i]That's an [b]awsome[/b] idea..unfortuantly..im no help[/i]
This forum sucks for displaying regexps. Bloody slash removal. Make sure you have slashes before your square brackets, etc etc....You're also missing the subject parameter of preg_replace.
[i]mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )[/i]
I have a feeling the [/ PHP] tag will cause an error in that slash - your delimiter is also a forward slash. I'm not sure if this causes an error or not, but I might suggest changing the delimiter to a hash or something.
___________________
Adam Goossens -- PHP is my mother tounge.
Linux: ( kernel.org | winehq ) -- f33l the p0w3r.
Nobody replying to your questions? Getting flamed? Getting told to RTFM? Ask your questions the right way.

November 13th, 2002
06:12 AM
GQ wannabe - lol
Status: Offline!
LOL at Adman - somebody stressed at work??
awesome, not awful.... 
___________________
Site: Rynoweb.com

November 13th, 2002
07:05 AM
save this for later
<pre>
<?
$str = 'Here is a php code {PHP] <? echo "help"; ?> [/PHP]. Cool eh?? Here are some more {PHP] <?
preg_match(\'/(\[PHP\])(.+?)(\[\/PHP\])/si\',$str,$match);
$c = $match[2];
highlight_string($c);
?>[/PHP] ';
preg_match_all('/(\{PHP\])(.+?)(\[\/PHP\])/si',$str,$match);
$codes = $match[2];
foreach($codes as $c){
$str = str_replace($c,highlight_string($c,TRUE),$str);
}
print $str;
?>
</pre>
change { to [ before testing
___________________
http://celerondude.com

November 13th, 2002
07:10 AM
now just figure out how to remove the tags in the hightlighted string
___________________
http://celerondude.com

November 13th, 2002
07:15 AM
done, here
<pre>
<?
$str = 'Here is a php code {PHP} <? echo "help"; ?> {/PHP]. Cool eh?? Here are some more {PHP} <?
preg_match(\'/(\[PHP\])(.+?)(\[\/PHP\])/si\',$str,$match);
$c = $match[2];
highlight_string($c);
?>{/PHP] ';
preg_match_all('/(\[PHP\])(.+?)(\[\/PHP\])/si',$str,$match);
$codes = $match[2];
foreach($codes as $i => $c){
$a = $match[1][$i];
$b = $match[3][$i];
$str = str_replace("$a$c$b",highlight_string($c,TRUE),$str);
}
print $str;
?>
</pre>
___________________
http://celerondude.com

November 13th, 2002
07:51 AM
function parsePHP( $string )
{
return preg_replace("/(?:\[php\])[\r\n]*(.*)(?:\[\/php\])/esi", "highlight_string('\\1', TRUE)", $string);
}
Highlighted in [code] tags due to [php] stripping tags.
Edit: See my below code for a better working version.
Last edited by Recessive, November 13th, 2002 09:52 AM (Edited 1 times)