linux php 语法加亮,用js函数PHP语法加亮
//=====================================
// 功能 PHP语法加亮函数
// author:ice_berg16(寻梦的稻草人)
// lastModified:2005-6-29
// copyright(c)2005 ice_berg16@163.com
//=====================================
function highlight_string( str )
{
//add a new prototype function to array
Array.prototype.exist = function(v)
{
for(k=0;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
{
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
");
res = '
- ' + res + '
';
//alert(res);
return res;
}
//对字符串中的HTML代码编码
function HTMLEncode( str )
{
str = str.replace(/&/g, '&');
str = str.replace(/
str = str.replace(/>/g, '>');
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[j] + ""; } return arr.join("/n"); } else { str = ""+ str + ""; return str; }}
linux php 语法加亮,用js函数PHP语法加亮相关推荐
- 100多个基础常用JS函数和语法集合大全
网站特效离不开脚本,javascript是最常用的脚本语言,我们归纳一下常用的基础函数和语法: 1.输出语句:document.write(""); 2.JS中的注释为// 3.传 ...
- c语言函数大全语法下载,C语言函数大全(语法).doc
C语言函数大全(语法) 函数名: abort功 能: 异常终止一个进程用 法: void abort(void);程序例:#include #include int main(void) { prin ...
- PHP用户自定义函数的语法结构,调用自定义函数(PHP语法)
公共提交参数,每个命令都需要提交以下公共参数 请求地址:管理后台-软件列表中获取对应软件API地址 请求类型:post 提交参数:data=URL编码后密文&sign=sign 参数名 参数说 ...
- 应用ast抽象语法树修改js函数
原理:AST抽象语法树 目标:在每一个函数里面插入一个console.log()把函数传入的全部参数输出出来 关于:本文章是在基于我的个人理解且怕忘记知识所记录下来的给自己看并且分享自己的一个心得,文 ...
- JS对象 数组排序sort() sort()方法使数组中的元素按照一定的顺序排列。 语法: arrayObject.sort(方法函数)...
数组排序sort() sort()方法使数组中的元素按照一定的顺序排列. 语法: arrayObject.sort(方法函数) 参数说明: 1.如果不指定<方法函数>,则按unicode码 ...
- api日常总结:前端常用js函数和CSS常用技巧
我的移动端media html{font-size:10px} @media screen and (min-width:321px) and (max-width:375px){html{font- ...
- 【Node.js】 基础语法
目录 Node.js npm package.json 字符编码 REPL Buffer(缓冲区) 函数 回调函数 模块系统 Express 框架 全局对象 console process Strea ...
- 详解Makefile 函数的语法与使用
使用函数: 在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函数的返回值可以当做变量来使 ...
- JS函数表达式——函数递归、闭包
一:定义函数的方式: 1.函数声明:2.函数表达式 函数声明的重要特征:函数声明提升,在执行代码之前会先读取函数声明. sayHi(); function sayHi(){console.log(&q ...
最新文章
- lte 中crs_LTE网络CRS功率配置及其影响研究
- python六十: __doc__属性
- 物理主机安装linux的方法
- Windows 下OpenSSL 安装详解 +图解
- php 数组插入键和值,php数组中键和值的关系
- $.when.apply_When2Meet vs.LettuceMeet:UI和美学方面的案例研究
- linux图形开发工具
- hdu 1507(最大匹配)
- Bz1621.lzh二进制编辑器下载
- vs2005无法附加进程
- AD555计算机辅助设计,震旦Aurora AD555 驱动
- 详解推荐系统的算法与应用
- 信息系统集成监理费收取标准_信息系统工程监理与咨询服务收费参考标准起草说明...
- 企业微信员工离职后,还能看聊天记录吗?
- 计算化学对计算机知识的要求,计算化学软件对大学有机化学教学的应用
- 大一php,大一总结 - 我的大一 - php中文网博客
- html下划线输入框自动延伸,纯CSS实现从中间延展的下划线
- 控制器正、反作用的确定
- 生产服务器硬盘寻道时间,HD Tune平均读写/寻道时间对比_希捷 NAS HDD 4TB 5900转 64MB_内存硬盘评测-中关村在线...
- 各种版本谷歌浏览器下载
热门文章
- RequestsLibrary库入门介绍
- eclipse启动优化文章集合
- 【数据结构笔记08】哨兵查找、二分查找、树、儿子-兄弟表示法、二叉树的引子
- linux软件中心无法安eclipse,Ubuntu软件中心安装Eclipse无法启动的问题
- EXT--表单AJax提交后台,返回前端数据格式的转换
- Java线程学习笔记(两) 线程异常处理
- 程序员在编程中遇到的奇葩弱智问题(转)
- java计算两个字符串格式的时间间隔多少天多少小时多少分钟
- Oracle ORA-06861: 文字与格式字符串不匹配 记录两次无助的遭遇:存储过程直接执行正常,java调用存储过程则报错
- c mysql 包含字符串_Mysql字符串字段判断是否包含某个字符串的2种方法