Jump to content


Photo

PHP Code Highlighting


  • Please log in to reply
6 replies to this topic

#1 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 17 July 2006 - 04:22 AM

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

#2 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 17 July 2006 - 04:23 AM

1. highlight a file = highlight_file('file.ext');
2. highlight a string = highlight_string($string);
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#3 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 17 July 2006 - 04:30 AM

Well here is an example of what i have:

<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>

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.

#4 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 17 July 2006 - 04:35 AM

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>
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#5 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 17 July 2006 - 04:38 AM

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.

#6 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 17 July 2006 - 04:44 AM

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.
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#7 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 17 July 2006 - 04:50 AM

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.

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users