`
jaketseng
  • 浏览: 40477 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

JS实现的PHP语法加亮函数

 
阅读更多

function highlight_string( str )
{
//add a new prototype function to array
Array.prototype.exist = function(v)
{
for(k=0;k<this.length;k++)
{
if(this[k].toLowerCase() == v.toLowerCase())
return true;
}
return false;
}

//base variable
var operator = "><=,()[].+-*/!&|^~?{};:";
var keyword= ['and','or','__FILE__','exception','__LINE__','array','as','break','case','class','const',
'continue','declare','default','die','do','echo','else','elseif','empty','enddeclare','endfor',
'endforeach','endif','endswitch','endwhile','eval','exit','extends','for','foreach','function',
'global','if','include','include_once','isset','list','new','old_function','print','require',
'require_once','return', 'static','switch','unset','var','while','__FUNCTION__','__CLASS__',
'__METHOD__','true','false','null'];
var inString = false;
var inSLComment = false; //single line comment
var inMLComment = false; //multiline comment
var delimiter = null;
var startPos = null;
var word= "";
var res = "";
//start to format
for(i=0;i<str.length;i++)
{
if( inString ) //we are in string
{
//the word cache will be clear
if(word != "") //we check the word cache if it the key word
{
if( keyword.exist(word) ) //its php reversed keyword,rend color
res+= rendColor(word, 'keyword');
else
res+= word;
word = "";
}
//alert('inString,pos is '+ i+',char is '+c );
fromPos = startPos+1;
while(1)
{
//we find the end of current string
p = str.indexOf( delimiter, fromPos );

//we got the end of the code
if( p == -1 )
{
curstr = str.substr( startPos );
res += rendColor( curstr, 'string' );
i = str.length;
break;
}
if( p != -1 && str.charAt(p-1) != "//" )
{
i = p+1;
curstr= str.substring(startPos, i ); //get the current string
res += rendColor( curstr, 'string' ); //rend color for it and add it to the result
inString = false; //we have go out of the string
startPos = null;
break;
}
else
{
fromPos = p+1;
}
}
}
if( inSLComment ) //we are in Single line comment
{
if(word != "") //we check the word cache if it the key word
{
if( keyword.exist(word) ) //its php reversed keyword,rend color
res+= rendColor(word, 'keyword');
else
res+= word;
word = "";
}
//alert('inSLComment,pos is '+ i+',char is '+c );
p = str.indexOf( "/n", i );
if( p != -1 ) //we find the end of line
{
i = p;
curstr = str.substring( startPos, p );
res += rendColor( curstr, 'comment' );
startPos = null;
inSLComment = false;
}
else
{
curstr = str.substr( startPos );
res += rendColor( curstr, 'comment' );
i = str.length;
}
}
if( inMLComment ) //we are in multiline comment
{
if(word != "") //we check the word cache if it the key word
{
if( keyword.exist(word) ) //its php reversed keyword,rend color
res+= rendColor(word, 'keyword');
else
res+= word;
word = "";
}
//alert('inMLComment,pos is '+ i+',char is '+c );
p = str.indexOf( "*/", startPos+2 );
if( p != -1 ) //we find the end of line
{
i = p+2;
curstr = str.substring(startPos, i );
res += rendColor( curstr, 'comment' );
startPos = null;
inMLComment = false;
}
else
{
curstr = str.substr( startPos );
res += rendColor( curstr, 'comment' );
i = str.length;
}
}
var c= str.charAt(i); //current char
var nc = str.charAt(i+1);//next char
switch( c )
{
case '/':
if( nc == '*' ) // we go into the multiline comment
{
inMLComment = true;
startPos = i;
}
if( nc == "/" ) //we are in single line comment
{
inSLComment = true;
startPos = i;
}
//alert('we are in switch,pos is '+i+', and char is'+ c);
break;
case '#':
inSLComment = true; //we go into the single line comment
startPos = i;
break;
case '"':
inString = true;
delimiter = '"';
startPos = i;
break;
case "'":
inString = true;
delimiter = "'";
startPos = i;
break;
default:
if( /[/w$]/.test(c) )//the keyword only contains continuous common char
{
word += c; //cache the current char
}
else
{
if(word != "") //we check the word cache if it the key word
{
if( keyword.exist(word) ) //its php reversed keyword,rend color
res+= rendColor(word, 'keyword');
else
res+= word;
word = "";
}
//now the current char is not common char, we process it
if( operator.indexOf(c) != -1 ) // the char is a operator
res += rendColor(c, 'operator' );
else
res += c;
}
break;
}
}
$t = "";
$b = " ";
res = res.replace(/^( +)/g, function($1){c = $1.length;str="";while(--c>=0)str+=$b;return str});
res = res.replace(/(/t| ){2,}/g, function($0){c=$0.length;str="";while(--c>=0){if($0.charAt(c)=='/t')str+=$t;else str+=$b;}return str;});
res = res.replace(//t/g,$t);
res = res.replace(//n/g, "/n</li><li>");
res = '<ol><li>' + res + '</li></ol>';
//alert(res);
return res;
}
//对字符串中的HTML代码编码
function HTMLEncode( str )
{
str = str.replace(/&/g, '&');
str = str.replace(/</g, '&lt;');
str = str.replace(/>/g, '&gt;');
return str;
}
//根据字符串所属类型渲染不同的着色
function rendColor( str, type )
{
var commentColor = "#FF8000";
var stringColor= "#DD0000";
var operatorColor= "#007700";
var keywordColor = "#007700";
var commonColor= "#0000BB";
var useColor= null;
str = HTMLEncode( str );

//we will rend what color?
switch( type )
{
case 'comment':
useColor= commentColor;
break;
case 'string':
useColor = stringColor;
break;
case 'operator':
useColor= operatorColor;
break;
case 'keyword':
useColor= keywordColor;
break;
default:
useColor= commonColor;
break;
}
if( str.indexOf("/n") != -1 ) //there are more than one line
{
arr = str.split("/n");
for(j=0;j<arr.length;j++)
{
arr[j] = "<span style='color:"+ useColor +"'>"+ arr[j] + "</span>";
}
return arr.join("/n");
}
else
{
str = "<span style='color:"+ useColor +"'>"+ str + "</span>";
return str;
}
}

分享到:
评论

相关推荐

    js函数与php函数的区别实例浅析

    在PHP语法中,函数就是语法上的结构体,不是一个变量,不能被赋值; 在JS中,函数也是一种变量,变量名就是函数名。 代码如下:&lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; [removed] function t(){  ...

    JavaScript Editor

    有着丰富的代码编辑功能(JavaScript, HTML, CSS, VBScript, PHP ,ASP(Net)语法加亮),内置预览功能,提供了完整的HTML 标记, HTML 属性, HTML 事件, JavaScript 事件和JavaScript 函数等完整的代码库,可轻松插入到...

    PHP100视频教程全集112集BT种子【PHP经典】

    PHP100视频教程41:PHP站内搜索、多关键字、加亮显示 PHP100视频教程42:PHP通过mail()或Socket发邮件 PHP100视频教程43:PHP中MVC学习之ThinkPHP(上) PHP100视频教程44:PHP中MVC学习之ThinkPHP(下) PHP100...

    PHP正则表达式基本语法和使用方法

    在PHP中支持PCRE(Perl Compatible Regular Expression)和POSIX(Portable Operation System interface)两套正则表达式处理函数,两套函数库功能相似,在执行效率上PCRE略占优势,因此自PHP 5.3.0版本以后POSIX正则...

    JavaScript Editor Pro 5.1 完美破解 智能提示

    JavaScript 脚本编辑软件,有着丰富的代码编辑功能(JavaScript, HTML, CSS, VBScript, PHP ,ASP(Net)语法加亮),内置预览功能,提供了完整的HTML 标记, HTML 属性, HTML 事件, JavaScript 事件和JavaScript 函数等...

    ZendStudio_13.01

    Zend Studio 13是一个屡获大奖的专业 PHP 集成开发环境,具备功能强大的专业编辑工具和调试工具,支持PHP语法加亮显示,支持语法自动填充功能,支持书签功能,支持语法自动缩排和代码复制功能,内置一个强大的PHP...

    php挑战题答案程序及代码

    2.文本处理:使用PHP 给 JS脚本语法上色并输出到浏览器! 3.使用Javascript 创建一个可以自由拖动及拉伸的DIV对话窗口层。 4.MYSQL创建两个一样的表 可以使用 MYSQL 部分中的表表结构 然后 写一条查询语句整合到一...

    js-markdown-extra:PHP-Markdown-额外兼容的 Javascript Markdown 语法解析器

    js-markdown-extra 是 PHP Markdown Extra 的实验性 JavaScript 端口。 由于 PHP 的正则表达式和 JavaScript 的正则表达式不同,我无法保留完全的兼容性,但它可以转换大多数简单的 Markdown 文本。 也许。 演示 你...

    经典php入门学习提纲

    在语法上php跟javascript也有很多相似之处,有没有觉得? 跟其它强类型定义语言比起来,php的的使用非常灵活,但是这种灵活却使php显得不那么严谨了,凡是都有利弊吧,这部 分我大概花了两天时间看了一下。 2,php...

    Gvim前端配置及php语法高亮,自动补全

    Gvim前端配置及php语法高亮,护眼背景,自动补全,已载入朴韩屏函数词典,及扩展的大量php,js,css等代码片段,是前段开发的神器,无需安装,解压即可使用。

    JavaScript for PHP Developers(中文版)第2版

    本书主要内容有讲解语法,包括变量、数组、循环和条件。学习中的函数为何重要,以及为什么它们实际上是对象。深入介绍的面向对象特性,包括原型、代码复用和继承。学习内建的API并了解其全局函数、属性和对象。学习...

    php课程(共100多节)

    41:PHP站内搜索、多关键字、加亮显示 42:PHP通过mail()或Socket发邮件 43:PHP中MVC学习之ThinkPHP(上) 44:PHP中MVC学习之ThinkPHP(下) 45:如何用PHP开发一个完整的网站 46:PHPMyAdmin功能操作详解 47:PHP...

    最好的JavaScript编辑工具 Javascript Editor 5.1

    JavaScript 脚本编辑软件,有着丰富的代码编辑功能(JavaScript, HTML, CSS, VBScript, PHP ,ASP(Net)语法加亮),内置预览功能,提供了完整的HTML 标记, HTML 属性, HTML 事件, JavaScript 事件和JavaScript 函数等...

    php网络开发完全手册

    第2章 PHP的基础语法 24 2.1 语言构成与工作原理 24 2.2 常量与变量 25 2.2.1 常量的定义 25 2.2.2 变量的定义 26 2.2.3 变量的作用域 27 2.2.4 动态变量 29 2.3 运算符和关键字 29 2.4 流程控制语法 30 2.4.1 程序...

    PHP和MySQL Web开发第4版pdf以及源码

    1.17 使用可替换的控制结构语法 1.18 使用declare 1.19 下一章 第2章 数据的存储与检索 2.1 保存数据以便后期使用 2.2 存储和检索Bob的订单 2.3 文件处理 2.4 打开文件 2.4.1 选择文件模式 2.4.2 使用...

    PHP 实现页面跳转的多种方式

    语法上需要注意 Location: 后面有一个空格,用法上需要格外注意:使用 Header 函数跳转页面时,切忌放在顶部,该语句前面有 HTML 的话,会直接报错。 二、使用 HTML 标记 其实就是使用 META 的 REFRESH 标记。 &lt...

    PHP和MySQL WEB开发(第4版)

    1.17 使用可替换的控制结构语法 1.18 使用declare 1.19 下一章 第2章 数据的存储与检索 2.1 保存数据以便后期使用 2.2 存储和检索Bob的订单 2.3 文件处理 2.4 打开文件 2.4.1 选择文件模式 2.4.2 使用fopen()打开...

    韩顺平PHP JS JQUERY 所有视频下载种子 货真价实

    9-30 2 javascript的闭包 js变量作用域 9-30 3 仿超级玛丽兄弟游戏制作 9-30 4 构造方法 对象的常用操作 9-30 5 面向对象的封装 继承 多态 9-30 6 面向对象的封装 继承 多态2 9-5 1.php xml编程①-xml基本介绍 xml...

Global site tag (gtag.js) - Google Analytics