Home  >  Article  >  Backend Development  >  Example sharing of simple source code syntax highlighting in PHP

Example sharing of simple source code syntax highlighting in PHP

黄舟
黄舟Original
2017-08-16 17:18:122366browse

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 = &#39;
// 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;&#39;;

// output the formatted code:
print &#39;<pre class="brush:php;toolbar:false">&#39;;
print syntax_highlight($example_php_code); //输出
print &#39;
'; ?>

Run the file and the result will be as shown below:

Example sharing of simple source code syntax highlighting in PHP

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn