Syntax Highlighting for Php

Highlight your php code in your website or in your blog post

Instead of using external tools or third party code to highlight the syntax of php code in your website you can use the inbuilt function of php

highlight_string or highlight_file

Highlight_string is the function to highlight the php syntax of the block of code

Here is the example for the highlight_string

<?php
$code = '
<?php
//array with files to allow
$allowed = array("index.php", "menu.php", "about.php");
 // get the filename
$file = $_GET[file];
if(file_Exists($file)){
if(in_array($file, $allowed)){
// check if it is part of the allowed list
highlight_file($file); //highlight file
}
else{
// not allowed. just die. do not warn ;)
die("");
}}
else{
// file doesnt exist
echo "The file does not exist.";
}
?>
';
highlight_string($code);
?>

highlight_file is the function to highlight the php syntax of the external file

Here is the example for the highlight_file

<?php
//array with files to allow
$allowed = array('index.php', 'menu.php', 'about.php');
// get the filename
$file = $_GET[file];
if(file_Exists($file)){
if(in_array($file, $allowed)){
// check if it is part of the allowed list
highlight_file($file); //highlight file
}
else{
// not allowed. just die. do not warn ;)
die("");
}
}
else{
// file doesnt exist
echo "The file does not exist.";
}
?>

For the demo of this code check this link http://phpfiddle.org/main/code/zf1-yjx and press F9 Key

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading