April 14, 2013 asvignesh 0Comment

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

 

Software developer experience in building storage management and Data protection software for the Private and public cloud.

Disclaimer :

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. my thoughts and opinions change from time to timeโ€ฆ This weblog is intended to provide a semi-permanent point in time snapshot and manifestation of the various memes running around my brain, and as such any thoughts and opinions expressed within out-of-date posts may not the same, nor even similar, to those I may hold today.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *