I looked it up, but still i find it weird; a var with a number..
Printable View
I looked it up, but still i find it weird; a var with a number..
It is right, since variables with the first character a number is (partly) reserved for this purpose. The preg function fills these according to what you indicate that should be put in. Pretty straightforward, though I would've done it differently, like with registrers in the ereg functions.
Grr I just can't get my head around this. The problem isn't singling out the code tags, it's using the highlight_string. I can't be stored in a variable. And the only way I see around this problem is not using the preg_replace till the actual content is generated. Which I want to avoid because it's just ugly code.
Does anyone have some (good) suggestions?
You can use preg_match aswell, getting the positions and lengths of the code that needs to be highlighted. Then replace them with the highlighted variants and convert the bbtags into html code.
Lots os possibilities mate ;), but none of them are really easy. Easier then in C though :p
Yeah, I'm using preg_match. I have this piece of code:
And this immediately out puts "<?php echo "test"; ?>" which is not really what should happen, the coloured text should just be stored in the variable. And not be outputted.PHP Code:<?php
$str = "[code]<?php echo 'test'; ?>[/code]";
$str = preg_match("/\[code\](.*?)\[\/code\]/is", $str, $matches);
$code = highlight_string($matches[1]);
?>
Haven't tested it, but I believe this works.PHP Code:<?php
$str = "[code]<?php echo 'test'; ?>[/code]";
$i_max = preg_match_all("/\[code\](.*?)\[\/code\]/is", $str, $matches, PREG_OFFSET_CAPTURE);
for ($i = 0; $i < $i_maxl $i++)
{
$str = substr_replace($str, highlight_string($matches[$i][0]), $matches[$i][1], strlen($matches[$i][0]));
}
$str = preg_replace('/\[code\](.*?)\[\/code\]/is', '<div class="code"><u><b>Code:</b></u>$1</div>', $str);
?>
PS: PHP has even build-in BBcode functions: http://nl.php.net/manual/en/book.bbcode.php
You forgot one semicolon (;), it'll error you.
It's in the 'for' loop, second part (for ($i = 0; $i < $i_max1; $i++).
Oh I get what you are saying:
http://us3.php.net/manual/en/functio...ght-string.php
This says, it will output OR return the result.
So you were close:Quote:
mixed highlight_string ( string $str [, bool $return ] )
If return is set to TRUE, returns the highlighted code as a string instead of printing it out. Otherwise, it will return TRUE on success, FALSE on failure.
PHP Code:<?php
$str = "[code]<?php echo 'test'; ?>[/code]";
$str = preg_match("/\[code\](.*?)\[\/code\]/is", $str, $matches);
$code = highlight_string($matches[1], true);
?>
I did that and got it into the variable.
Heh, you're awesome madi. :D
If you dont mind im gonna take some of Pie & Madi's code and make some little page where i also test my JS skills =p
I wanna © if u find it needed?
Heh those 10 lines are pretty insignificant, my blogpack has 5000+ lines of code.
I'll send you the source if you don't believe me. :tongue: