View Full Version : Displaying PHP Code

03-09-2003, 11:28 AM
How would u display PHP Coding on a page

like you've got on here?

Like this

03-09-2003, 12:51 PM
A nice simple way would be to use view-source("page.php")

03-09-2003, 12:53 PM
hmm, could try that

but is there a way 2 display wot i type in myself? not display a ceratin page

03-09-2003, 12:58 PM
Only way I can think is to use str_replace, like

$string = str_replace("<?php","<font color='blue'>&amp;lt;?php</font>",$string);

03-09-2003, 01:02 PM
how would i then use that 2 display though?

btw, view-source didnt work, came up wiv an error. searched for it on PHP.net & couldnt find it. cheers ne way

03-09-2003, 01:09 PM
$string would be the info that you want highlighted, for example

$string = "<?php echo "Yak"; ?>";
// now go through the string and highlight various parts
$string = str_replace("<?php","<font color='blue'>&amp;lt;?php</font>",$string);
$string = str_replace("echo","<font color='orange'>echo</font>",$string);

echo $string;

Obviously there'll need more work on it, like reg exp being used on different keywords, but you get the idea, i hope :)

03-09-2003, 01:13 PM
right... well i pasted that code straight in 2 see wot it comes up wiv on my page & it came up wiv this....

Parse error: parse error, unexpected T_STRING in /home/gtaonet/public_html/community/mysql/basic.php on line 69

which is this line:

$string = "<?php echo "Yak"; ?>";

u know y?

i get the feeling im jumping straight in2 deep water & drwowning

btw.... the site can be found here http://community.gtao.net

03-09-2003, 01:16 PM
oops, yeah I know, change "Yak" to 'Yak'

03-09-2003, 01:18 PM
lol, there we go, works now

so from that i should be ok? but u reckon i should look up reg exp? wot does that do exactly?

03-09-2003, 01:25 PM
What I gave you will be the very basics, reg exp would highlight things properlylike echo "dfdfsf" would only result in echo " " being highlighted one color and the dfdfsf being in another colour, you can't do that with str_replace. I'd show an example of reg exp, but I hate it and don't know it that well :p

03-09-2003, 01:26 PM
k, fair enough

ill have a look 4 it then

cheers 4 the help

03-09-2003, 01:32 PM
$str = '<?php echo 'blat'; // comment ?>';

$disp_str = highlight_string($str);
echo $disp_str;

03-09-2003, 01:36 PM
Ooh, never knew about that one :) Thought there must've been an easier way

03-09-2003, 01:37 PM
yeah, that works pretty much

but it so small i cant read it, just see the colours

how would i change the font size?

03-09-2003, 01:43 PM
not 2 worry, i figured that bit out

the only prob i have now is that it wont display things like


well ne thing wiv $ in front of it

how do i get round that?

03-09-2003, 01:57 PM
never mind.... its showing them now

cheers 4 ur help ppl

03-09-2003, 05:39 PM
hmmm.... out of interest, i was looking through my PHP info and found this:

highlight.bg #FFFFFF #FFFFFF
highlight.comment #FF9900 #FF9900
highlight.default #0000CC #0000CC
highlight.html #000000 #000000
highlight.keyword #006600 #006600
highlight.string #CC0000 #CC0000

does that have anything to do with highlighting php syntax?

03-09-2003, 06:04 PM
Yeah, they''re the colours used in the highlighting

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum