您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)利用php怎么實現(xiàn)一個語法高亮功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
function syntax_highlight($code){ // this matches --> "foobar" <-- $code = preg_replace( '/"(.*?)"/U', '"<span >$1</span>"', $code ); // hightlight functions and other structures like --> function foobar() <--- $code = preg_replace( '/(\s)\b(.*?)((\b|\s)\()/U', '$1<span >$2</span>$3', $code ); // Match comments (like /* */): $code = preg_replace( '/(\/\/)(.+)\s/', '<span ><i>$0</i></span>', $code ); $code = preg_replace( '/(\/\*.*?\*\/)/s', '<span ><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 >$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 >$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怎么實現(xiàn)一個語法高亮功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。