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: #20.b3"> $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 */ |