国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php實現簡單的語法高亮函數實例分析

2024-05-04 23:34:46
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php實現簡單的語法高亮函數,實例分析了php通過正則表達式實現語法高亮的相關技巧,需要的朋友可以參考下

本文實例講述了php實現簡單的語法高亮函數。分享給大家供大家參考。具體分析如下:

這是一個php實現的簡單語法高亮顯示的函數,注意:這個函數設計的比較簡單,可能對某些語法不能高亮顯示,你可以自己擴充該函數的功能

 

 
  1. function syntax_highlight($code){ 
  2. // this matches --> "foobar" <-- 
  3. $code = preg_replace( 
  4. '/"(.*?)"/U',  
  5. '"<span style="color: #007F00">$1</span>"'$code 
  6. ); 
  7. // hightlight functions and other structures like --> function foobar() <---  
  8. $code = preg_replace( 
  9. '/(/s)/b(.*?)((/b|/s)/()/U',  
  10. '$1<span style="color: #0000ff">$2</span>$3',  
  11. $code 
  12. ); 
  13. // Match comments (like /* */):  
  14. $code = preg_replace( 
  15. '/(////)(.+)/s/',  
  16. '<span style="color: #660066; background-color: #FFFCB1;"><i>$0</i></span>',  
  17. $code 
  18. ); 
  19. $code = preg_replace( 
  20. '/(///*.*?/*//)/s',  
  21. '<span style="color: #660066; background-color: #FFFCB1;"><i>$0</i></span>',  
  22. $code 
  23. ); 
  24. // hightlight braces: 
  25. $code = preg_replace('/(/(|/[|/{|/}|/]|/)|/->)/''<strong>$1</strong>'$code); 
  26. // hightlight variables $foobar 
  27. $code = preg_replace( 
  28. '/(/$[a-zA-Z0-9_]+)/''<span style="color: #0000B3">$1</span>'$code 
  29. ); 
  30. /* The /b in the pattern indicates a word boundary, so only the distinct 
  31. ** word "web" is matched, and not a word partial like "webbing" or "cobweb"  
  32. */ 
  33. // special words and functions 
  34. $code = preg_replace( 
  35. '//b(print|echo|new|function)/b/',  
  36. '<span style="color: #7F007F">$1</span>'$code 
  37. ); 
  38. return $code
  39. /*example-start*/ 
  40. /* 
  41. ** Create some example PHP code: 
  42. */ 
  43. $example_php_code = ' 
  44. // some code comment: 
  45. $example = "foobar"
  46. print $_SERVER["REMOTE_ADDR"]; 
  47. $array = array(1, 2, 3, 4, 5); 
  48. function example_function($str) { 
  49. // reverse string 
  50. echo strrev($obj); 
  51. print example_function("foo"); 
  52. /* 
  53. ** A multiple line comment 
  54. */ 
  55. print "Something: " . $example;'; 
  56. // output the formatted code: 
  57. print '<pre>'
  58. print syntax_highlight($example_php_code); 
  59. print '</pre>'
  60. /*example-end*/ 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀集县| 格尔木市| 西平县| 津市市| 逊克县| 上林县| 温泉县| 子长县| 兴安县| 石渠县| 万安县| 南澳县| 松阳县| 芷江| 岑巩县| 商河县| 乌恰县| 梅河口市| 沙河市| 饶平县| 河间市| 西贡区| 双鸭山市| 西充县| 区。| 云阳县| 宝应县| 莲花县| 宾阳县| 锦州市| 庄浪县| 自治县| 凌云县| 定襄县| 毕节市| 阿克苏市| 武义县| 闽侯县| 册亨县| 常宁市| 山阳县|