View Full Version : PHP Code colouring
04-15-2003, 08:28 PM
Right, I hope I am posting this in the right place. I assume I am cos I think I will need PHP RegEx.
As on this board you get PHP code colouring. Is there a way that this could be done? Im not that good with regEx.
Any help appreciated.
04-15-2003, 08:41 PM
PHP has a built in function for this.
Have a look here (http://www.php.net/manual/en/function.highlight-string.php).
04-15-2003, 08:44 PM
Thanks for the link! That was alot easier than I was expecting! :D
04-15-2003, 09:06 PM
Right, that works. Now, (this is why I thought RegExp) how do I make all text inside a [php] tag coloured in that way?
04-16-2003, 06:15 PM
http://www.php.net/manual/en/function.ereg.php would be the function you using.
Could go something like this:
if (ereg('[php](.*)[/ php]',$string))
highlight your code
*Note, there is a gap between the / and php, that's just cause the format came out messed up if they were linked up together.
04-17-2003, 04:07 PM
Sorry, that doesnt seem to work.
04-17-2003, 06:26 PM
First, don't use the ereg functions, use the preg ones. 2nd, [ and ] should be escaped in a pattern, otherwise they dfine a character set
$someString = preg_replace( "/\\[php](.*)\\[/ php]/i", highlight_string( "$1" ), $someString );That should work
Note: remove the space in [/ php]
04-17-2003, 07:06 PM
What is the variable 1, is that defined in the (.*) bit?
PS: I knew the  should be unescaped, so I tried that too.
Thanks beetle, Ill give it a go.
04-17-2003, 07:23 PM
$1 is not a variable per se, but a backreference to the 1st matched parenthical in the pattern. So, everything matched by (.*) is retrieved in the replace argument with "$1"
04-17-2003, 07:41 PM
Sorry, still no luck.
Here is my code
$_POST['code'] = preg_replace( "/\[php\](.*)\[\/ php\]/i", highlight_string( "$1" ), $_POST['code'] );
04-17-2003, 07:42 PM
I have \\ behind all the [,], and /, But vB strips them out.
04-17-2003, 07:49 PM
Can you write to a superglobal array like that? I'm not sure, but I think those are read-only. Use another variable to store the result
04-17-2003, 09:03 PM
Ok, I'll have another go thanks.