国产探花免费观看_亚洲丰满少妇自慰呻吟_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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嫩江县| 武清区| 高碑店市| 安泽县| 九龙城区| 班戈县| 手机| 永修县| 锦州市| 宣化县| 六枝特区| 娄烦县| 博湖县| 木里| 怀化市| 福鼎市| 小金县| 濮阳市| 信宜市| 富源县| 湖北省| 和林格尔县| 阳信县| 任丘市| 西林县| 岳普湖县| 罗田县| 牙克石市| 东宁县| 宁国市| 林州市| 乐清市| 江西省| 满洲里市| 顺平县| 永顺县| 锦屏县| 梧州市| 社旗县| 富锦市| 方正县|