Php implements simple syntax highlighting functions
|
A simple syntax highlighted function implemented by php. note: This function is easy to design and may not be highlighted for some syntaxes. you can expand the function by yourself.
- Function syntax_highlight ($ code ){
- // This matches --> "foobar" <--
- $ Code = preg_replace (
- '/"(.*?) "/U ',
- '"$1"', $ code
- );
- // Hightlight functions and other structures like --> function foobar () <---
- $ Code = preg_replace (
- '/(\ S) \ B (.*?) (\ B | \ s) \ ()/U ',
- '$1 $2 $3 ',
- $ Code
- );
- // Match comments (like /**/):
- $ Code = preg_replace (
- '/(\/) (. +) \ S /',
- '$0',
- $ Code
- );
- $ Code = preg_replace (
- '/(\/\*.*? \ * \/)/S ',
- '$0',
- $ Code
- );
- // Hightlight braces:
- $ Code = preg_replace ('/(\ (| \ [|\{|\}|\] |\) |\-> )/','$1', $ Code );
- // Hightlight variables $ foobar
- $ Code = preg_replace (
- '/(\ $[A-zA-Z0-9 _] +)/', '$ 1', $ 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 /',
- '$ 1', $ 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'
'; - print syntax_highlight($example_php_code);
- print '
';
- /* Example-end */
|
Php