php实现简单的语法高亮函数实例分析


Posted in PHP onApril 27, 2015

本文实例讲述了php实现简单的语法高亮函数。分享给大家供大家参考。具体分析如下:

这是一个php实现的简单语法高亮显示的函数,注意:这个函数设计的比较简单,可能对某些语法不能高亮显示,你可以自己扩充该函数的功能

function syntax_highlight($code){
 // this matches --> "foobar" <--
 $code = preg_replace(
  '/"(.*?)"/U', 
  '"<span style="color: #007F00">$1</span>"', $code
 );
 // hightlight functions and other structures like --> function foobar() <--- 
 $code = preg_replace(
  '/(\s)\b(.*?)((\b|\s)\()/U', 
  '$1<span style="color: #0000ff">$2</span>$3', 
  $code
 );
 // Match comments (like /* */): 
 $code = preg_replace(
  '/(\/\/)(.+)\s/', 
  '<span style="color: #660066; background-color: #FFFCB1;"><i>$0</i></span>', 
  $code
 );
 $code = preg_replace(
  '/(\/\*.*?\*\/)/s', 
  '<span style="color: #660066; background-color: #FFFCB1;"><i>$0</i></span>', 
  $code
 );
 // hightlight braces:
 $code = preg_replace('/(\(|\[|\{|\}|\]|\)|\->)/', '<strong>$1</strong>', $code);
 // hightlight variables $foobar
 $code = preg_replace(
  '/(\$[a-zA-Z0-9_]+)/', '<span style="color: #0000B3">$1</span>', $code
 );
 /* The \b in the pattern indicates a word boundary, so only the distinct
 ** word "web" is matched, and not a word partial like "webbing" or "cobweb" 
 */
 // special words and functions
 $code = preg_replace(
  '/\b(print|echo|new|function)\b/', 
  '<span style="color: #7F007F">$1</span>', $code
 );
 return $code;
}
/*example-start*/
/*
** Create some example PHP code:
*/
$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>';
print syntax_highlight($example_php_code);
print '</pre>';
/*example-end*/

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
Content-type 的说明
Oct 09 PHP
利用js调用后台php进行数据处理原码
Oct 09 PHP
怎样在php中使用PDF文档功能
Oct 09 PHP
php函数的常用方法及注意之处小结
Jul 10 PHP
php的hash算法介绍
Feb 13 PHP
php jsonp单引号转义
Nov 23 PHP
PHP生成json和xml类型接口数据格式
May 17 PHP
日常整理PHP中简单的图形处理(经典)
Oct 26 PHP
php使用PDO执行SQL语句的方法分析
Feb 16 PHP
分享5个非常有用的Laravel Blade指令
May 30 PHP
基于Laravel 多个中间件的执行顺序详解
Oct 21 PHP
PHP实现提取多维数组指定一列的方法总结
Dec 04 PHP
php转换颜色为其反色的方法
Apr 27 #PHP
PHP结合jQuery.autocomplete插件实现输入自动完成提示的功能
Apr 27 #PHP
PHP+jQuery+Ajax实现用户登录与退出
Apr 27 #PHP
php使用cookie实现记住用户名和密码实现代码
Apr 27 #PHP
php使用cookie实现记住登录状态
Apr 27 #PHP
php curl请求信息和返回信息设置代码实例
Apr 27 #PHP
PHP rsa加密解密使用方法
Apr 27 #PHP
You might like
图片存储与浏览一例(Linux+Apache+PHP+MySQL)
2006/10/09 PHP
PHP 循环删除无限分类子节点的实现代码
2013/06/21 PHP
PHP编程基本语法快速入门手册
2016/01/07 PHP
PHP中利用sleep函数实现定时执行功能实现代码
2016/08/25 PHP
php微信公众号开发之关键词回复
2018/10/20 PHP
可恶的ie8提示缺少id未定义
2014/03/20 Javascript
页面刷新时记住滚动条的位置jquery代码
2014/06/17 Javascript
使用AngularJS实现可伸缩的页面切换的方法
2015/06/19 Javascript
JS使用post提交的两种方式
2015/12/03 Javascript
angular 用拦截器统一处理http请求和响应的方法
2017/06/08 Javascript
使用vue-cli+webpack搭建vue开发环境的方法
2017/12/22 Javascript
layUI实现前端分页和后端分页
2019/07/27 Javascript
Vue开发环境中修改端口号的实现方法
2019/08/15 Javascript
微信小程序 子级页面返回父级并把子级参数带回父级实现方法
2019/08/22 Javascript
vue 实现走马灯效果
2019/10/28 Javascript
跟老齐学Python之一个免费的实验室
2014/09/14 Python
详解opencv Python特征检测及K-最近邻匹配
2019/01/21 Python
python flask安装和命令详解
2019/04/02 Python
Python3.5 Pandas模块之DataFrame用法实例分析
2019/04/23 Python
详解Python传入参数的几种方法
2019/05/16 Python
Python任务调度利器之APScheduler详解
2020/04/02 Python
python 绘制国旗的示例
2020/09/27 Python
用HTML5实现网站在windows8中贴靠的方法
2013/04/21 HTML / CSS
德国婴儿推车和儿童安全座椅商店:BABYSHOP
2016/09/01 全球购物
Lyle & Scott苏格兰金鹰官网:英国皇室御用品牌
2018/05/09 全球购物
linux系统都有哪些运行级别
2016/03/26 面试题
租房协议书范本
2014/04/09 职场文书
IT工程师岗位职责
2014/07/04 职场文书
企业委托书范本
2014/09/13 职场文书
2014年“四风”问题个人整改措施
2014/09/17 职场文书
群众路线教育实践活动个人对照检查材料
2014/09/22 职场文书
2015年世界粮食日演讲稿
2015/03/20 职场文书
领导离职感言
2015/08/03 职场文书
中小学教师继续教育心得体会
2016/01/19 职场文书
Windows 11要来了?微软文档揭示Win11太阳谷 / Win10有两个不同版本
2021/11/21 数码科技
在Windows Server 2012上安装 .NET Framework 3.5 所遇到的问题
2022/04/29 Servers