Jump to content

Archived

This topic is now archived and is closed to further replies.

adamwhiles

PHP Code Highlighting

Recommended Posts

I was wondering how to go about highlight example php code. I have a page where I show examples of how to use php code and I would like to highlight the php code in it, alot like phpfreaks does on their tutorial pages. I already have a box with the code in it that displays fine, I just want to color the syntax and format it a little. A point in the right direction would be much appreciated. Thanks

Share this post


Link to post
Share on other sites
1. highlight a file = highlight_file('file.ext');
2. highlight a string = highlight_string($string);

Share this post


Link to post
Share on other sites
Well here is an example of what i have:

[code]
<div id="php_code_header">PHP Code Example:</div>
<div id="php_code">
if (IsSet($_POST['Submit'])) {
<br>echo "The submit button was pressed";
<br>}
<br>else {
<br>echo "The submit button was not pressed";
<br>}
</div>
[/code]

The div's setup my box the code is contained in, and I want the rest to be highlighted. I'm not sure I understand how to go about this.

Share this post


Link to post
Share on other sites
I hope I understand what you're talking about.

Try this:
<div id="php_code_header">PHP Code Example:</div>
<div id="php_code">
<?php highlight_string($code_to_highlight); ?>
</div>

Share this post


Link to post
Share on other sites
I would need to set something like this:

$code = "echo \"Hello World!\";";
highlight_string($code);

I'm not sure how to properly keep the " in my code from causing problems, thats where I have trouble.

Share this post


Link to post
Share on other sites
Ok. 

I think there's a possibilty you are going about presenting the code the wrong way.  And by the way,  I love the avatar.  It sounds like you are either making a forum type system, or maybe just highlighting some could for tutorials.

Are you accepting a POSTed string and highlighting it, or are you purposely putting code inside a variable then printing it?

If you can tell me what and/or how your process works, I might be able to suggest a better situation for you.

Share this post


Link to post
Share on other sites
I figured it out through a little trial and error. Thanks for you help though. Glad you like my avatar. Here is the code I used in case anyone else needs help with this.

[code]
highlight_string('<? if (IsSet($_POST[\'Submit\'])) {
echo "The submit button was pressed";
}
else {
echo "The submit button was not pressed";
}
?>');
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.