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

首頁 > 語言 > PHP > 正文

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

2024-09-04 11:42:42
字體:
來源:轉載
供稿:網友

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

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

  1. function syntax_highlight($code){ 
  2.  // this matches --> "foobar" <-- 
  3.  $code = preg_replace( 
  4.   '/"(.*?)"/U',  
  5.   '&quot;<span style="color: #007F00">$1</span>&quot;'$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*/

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扶绥县| 丰镇市| 金川县| 冷水江市| 石门县| 韶关市| 福鼎市| 宁明县| 柘城县| 阳城县| 泸西县| 阿合奇县| 兴文县| 沈阳市| 赤壁市| 界首市| 东至县| 嘉义县| 平舆县| 崇信县| 上栗县| 鄯善县| 贵南县| 诸暨市| 岫岩| 西宁市| 米脂县| 安乡县| 阳谷县| 威海市| 茂名市| 双桥区| 丹阳市| 安泽县| 黑河市| 荔波县| 黔江区| 南木林县| 龙海市| 海南省| 新野县|