Home > Article > Backend Development > Example sharing of simple source code syntax highlighting in PHP
In our daily development work, we will find that many codes are highlighted in the display of the web page. The effect of this is to make the web page more beautiful and make the user experience of the web page more intuitive. So today We will introduce to you how to use php to achieve code highlighting effect!
First download the PHP we need to use for this course to implement a simple source code syntax highlighting function library: http://www.php.cn/xiazai/leiku/691
After the download is complete, find the php class file we need, unzip it to our local directory, and create a new php file!
After completion, we need to call this class in the new php file and instantiate the class:
<?php include_once "codegl.php";//引入文件 //定义 $example_php_code = ' // some code comment: $example = "foobar"; print $_SERVER["REMOTE_ADDR"]; $array = array(1, 2, 3, 4, 5); function example_function($str) { // reverse string echo strrev($obj); } print example_function("foo"); /* ** A multiple line comment */ print "Something: " . $example;'; // output the formatted code: print '<pre class="brush:php;toolbar:false">'; print syntax_highlight($example_php_code); //输出 print ''; ?>
Run the file and the result will be as shown below:
Explanation:
This function is relatively simple in design and may not be able to highlight certain syntaxes. , you can expand the function of this function yourself!
The above is the detailed content of Example sharing of simple source code syntax highlighting in PHP. For more information, please follow other related articles on the PHP Chinese website!