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 相关文章推荐
php a simple smtp class
Nov 26 PHP
PHP简单系统查询模块代码打包下载
Jun 07 PHP
php统计文章排行示例
Mar 04 PHP
zen_cart实现支付前生成订单的方法
May 06 PHP
Centos6.5和Centos7 php环境搭建方法
May 27 PHP
php版微信公众平台回复中文出现乱码问题的解决方法
Sep 22 PHP
PHP中使用OpenSSL生成证书及加密解密
Feb 05 PHP
thinkPHP显示不出验证码的原因与解决方法分析
May 20 PHP
PHP实现驼峰样式字符串(首字母大写)转换成下划线样式字符串的方法示例
Aug 10 PHP
PHP针对redis常用操作实例详解
Aug 17 PHP
PHP7 安装event扩展的实现方法
Oct 08 PHP
Laravel监听数据库访问,打印SQL的例子
Oct 24 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
山进SANGEAN ATS-909X电路分析
2021/03/02 无线电
php 实现进制转换(二进制、八进制、十六进制)互相转换实现代码
2010/10/22 PHP
PHP在获取指定目录下的目录,在获取的目录下面再创建文件,多平台
2011/08/03 PHP
领悟php接口中interface存在的意义
2013/06/27 PHP
基于PHP中的常用函数回顾
2013/07/11 PHP
ThinkPHP中session函数详解
2016/09/14 PHP
php函数式编程简单示例
2019/08/08 PHP
PHP终止脚本运行三种实现方法详解
2020/09/01 PHP
仿淘宝TAB切换搜索框搜索切换的相关内容
2014/09/21 Javascript
node.js中的http.get方法使用说明
2014/12/14 Javascript
js跨域资源共享 基础篇
2016/07/02 Javascript
EasyUI框架 使用Ajax提交注册信息的实现代码
2017/09/27 Javascript
微信小程序如何像vue一样在动态绑定类名
2018/04/17 Javascript
vue vue-Router默认hash模式修改为history需要做的修改详解
2018/09/13 Javascript
vue动态渲染svg、添加点击事件的实现
2020/03/13 Javascript
vue使用echarts实现水平柱形图实例
2020/09/09 Javascript
python 正则式使用心得
2009/05/07 Python
python中快速进行多个字符替换的方法小结
2016/12/15 Python
浅谈python正则的常用方法 覆盖范围70%以上
2018/03/14 Python
Python 查找字符在字符串中的位置实例
2018/05/02 Python
使用python对excle和json互相转换的示例
2018/10/23 Python
对python中的argv和argc使用详解
2018/12/15 Python
python flask框架实现重定向功能示例
2019/07/02 Python
Python中模块(Module)和包(Package)的区别详解
2019/08/07 Python
阿迪达斯丹麦官网:adidas丹麦
2016/10/01 全球购物
高级运动鞋:GREATS
2019/07/19 全球购物
Happy Plugs官网:瑞典无线耳机品牌
2020/07/16 全球购物
机械工程师求职自我评价
2013/09/23 职场文书
物流管理专业应届生求职信
2013/11/21 职场文书
应用心理学个人求职信范文
2013/12/11 职场文书
市场营销专业毕业生求职信
2014/03/26 职场文书
《真想变成大大的荷叶》教学反思
2014/04/14 职场文书
科长个人四风问题整改措施思想汇报
2014/10/13 职场文书
化验室岗位职责
2015/02/14 职场文书
「月刊Comic Alive」2022年5月号封面公开
2022/03/21 日漫
美国运营商 T-Mobile 以 117.83Mb/s 的速度排第一位
2022/04/21 数码科技