php實(shí)現(xiàn)簡(jiǎn)單的語法高亮函數(shù)實(shí)例分析
本文實(shí)例講述了php實(shí)現(xiàn)簡(jiǎn)單的語法高亮函數(shù)。分享給大家供大家參考。具體分析如下:
這是一個(gè)php實(shí)現(xiàn)的簡(jiǎn)單語法高亮顯示的函數(shù),注意:這個(gè)函數(shù)設(shè)計(jì)的比較簡(jiǎn)單,可能對(duì)某些語法不能高亮顯示,你可以自己擴(kuò)充該函數(shù)的功能
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*/
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- 9個(gè)javascript語法高亮插件 推薦
- js 玩轉(zhuǎn)正則表達(dá)式之語法高亮
- JavaScript建立一個(gè)語法高亮輸入框?qū)崿F(xiàn)思路
- 2016年最熱門的15 款代碼語法高亮工具,美化你的代碼
- jQuery語法高亮插件支持各種程序源代碼語法著色加亮
- Jquery.LazyLoad.js修正版下載,實(shí)現(xiàn)圖片延遲加載插件
- 一個(gè)簡(jiǎn)單的jQuery插件ajaxfileupload.js實(shí)現(xiàn)ajax上傳文件例子
- JavaScript/jQuery 表單美化插件小結(jié)
- 輕量級(jí)的原生js日歷插件calendar.js使用指南
- JavaScript語法高亮插件highlight.js用法詳解【附highlight.js本站下載】
相關(guān)文章
dedecms系統(tǒng)的廣告設(shè)置代碼 基礎(chǔ)版本
dedecms系統(tǒng)的廣告設(shè)置代碼 基礎(chǔ)版本,需要的朋友可以參考下。2010-04-04用Zend Studio+PHPnow+Zend Debugger搭建PHP服務(wù)器調(diào)試環(huán)境步驟
作為一名非專業(yè)的PHP開發(fā)者,想要較方便地讀懂并搞清楚一個(gè)PHP功能模塊的運(yùn)行細(xì)節(jié),搭建一個(gè)PHP調(diào)試環(huán)境,然后單步調(diào)試程序,以掌握程序的整個(gè)運(yùn)行過程顯的尤為重要2014-01-01用js進(jìn)行url編碼后用php反解以及用php實(shí)現(xiàn)js的escape功能函數(shù)總結(jié)
這次第一次用smarttemplate這個(gè)模板,比smarty小巧了很多,但也有些不方便的地方。2010-02-02