Recommended sites
Web Hosting Reviews

Syntax highlighted php

In this tutorial I will show you how to convert an existing php code to a syntax colored html text.

In this tutorial I will show you how to convert an existing php code to a syntax colored html text.

Many times when I want to display a short php code on my website I always have a small problem how to display it nice. You can not just insert it into the html code as the webserver might execute instead of displaying it.

To solve this problem you can use the built in php function: htmlspecialchars which converts the code to HTML entities.

Syntax: string htmlspecialchars ( string string [, int quote_style [, string charset]] )

Put your php code in a string variable and convert it. As example let see the following small php code: <?php echo "demo"; ?> .
If you want a valid HTML text which produce that output you should write something like this:

<?php
  $mycode
='<?php echo "demo"; ?>';
  echo 
htmlspecialchars($mycode);
?> 


The code above will display the following:

<?php echo "demo"; ?>

It is better but it can be much better. It would be much better with colors and fixed fonts (as my nice code :)).
You can add this nice color with an other built in function which is: highlight_string

Synatx: mixed highlight_string ( string str [, bool return] )

Before we apply this new function take a look on the second parameter.
If the second parameter return is set to TRUE then highlight_string() will return the highlighted code as a string instead of printing it out. If the second parameter is not set to TRUE then highlight_string() will return TRUE on success, FALSE on failure.

Now the final code is quite easy:

<?php 
$mycode
='<?php echo "demo"; ?>'; 
highlight_string($mycode); 
?>


And if you want to display the HTML code itself than use the following:

<?php 
$mycode
='<?php echo "demo"; ?>'; 
echo 
highlight_string($mycode,true); 
?> 


At the end here is a small code which converts any php code into a syntax colored HTML code. It is commented so it should not be a problem to understand it.

<html>
<body>
   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="syntax">
      PHP code to highlight:
      <table>
        <tr>
          <td>
            <textarea name="codeSrc" rows="10" cols="60"></textarea>
          </td>
        </tr>
        <tr>
          <td align="center"><br/>
            <input type="submit" name="submitBtn" value="Highlight it!">
          </td>
        </tr>
      </table>  
   </form>
   <br/>
<?php    
    
// Check whether the form was submitted
    
if (isset($_POST['submitBtn'])){
        
        
// Get the text from the textarea
        
$codeSrc = (isset($_POST['codeSrc'])) ? $_POST['codeSrc'] : '';
        
        
// Remove the added slashes
        
$codeSrc = stripslashes($codeSrc);
        
        
// Make the syntax coloring and than convert it to html text
        
$codeOut = (highlight_string($codeSrc,true));
        
        
// Display the result in a new textarea
        
echo '<table width="100%">
                <tr>
                  <td>
                    <textarea name="codeSrc" rows="10" cols="60">'
                    
.$codeOut.
                   
'</textarea>
                  </td>
                </tr>
              </table>'
;
    }        
?>
  </body>  
</html> 

Sponsored links