php 简单语法突出显示
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 简单语法突出显示相关的知识,希望对你有一定的参考价值。
<span style="color: #000000; font-weight: bold;">function</span> syntax_highlight<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$code</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// this matches --> "foobar" <--</span>
<span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/preg_replace"><span style="color: #000066;">preg_replace</span></a><span style="color: #66cc66;">(</span>
<span style="color: #ff0000;">'/"(.*?)"/U'</span>,
<span style="color: #ff0000;">'&quot;<span style="color: #007F00">$1</span>&quot;'</span>, <span style="color: #0000ff;">$code</span>
<span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// hightlight functions and other structures like --> function foobar() <--- </span>
<span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/preg_replace"><span style="color: #000066;">preg_replace</span></a><span style="color: #66cc66;">(</span>
<span style="color: #ff0000;">'/(<span style="color: #000099; font-weight: bold;">\s</span>)<span style="color: #000099; font-weight: bold;">\b</span>(.*?)((<span style="color: #000099; font-weight: bold;">\b</span>|<span style="color: #000099; font-weight: bold;">\s</span>)<span style="color: #000099; font-weight: bold;">\(</span>)/U'</span>,
<span style="color: #ff0000;">'$1<span style="color: #0000ff">$2</span>$3'</span>,
<span style="color: #0000ff;">$code</span>
<span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Match comments (like /* */): </span>
<span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/preg_replace"><span style="color: #000066;">preg_replace</span></a><span style="color: #66cc66;">(</span>
<span style="color: #ff0000;">'/(<span style="color: #000099; font-weight: bold;">\/</span><span style="color: #000099; font-weight: bold;">\/</span>)(.+)<span style="color: #000099; font-weight: bold;">\s</span>/'</span>,
<span style="color: #ff0000;">'<span style="color: #660066; background-color: #FFFCB1;"><i>$0</i></span>'</span>,
<span style="color: #0000ff;">$code</span>
<span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/preg_replace"><span style="color: #000066;">preg_replace</span></a><span style="color: #66cc66;">(</span>
<span style="color: #ff0000;">'/(<span style="color: #000099; font-weight: bold;">\/</span><span style="color: #000099; font-weight: bold;">\*</span>.*?<span style="color: #000099; font-weight: bold;">\*</span><span style="color: #000099; font-weight: bold;">\/</span>)/s'</span>,
<span style="color: #ff0000;">'<span style="color: #660066; background-color: #FFFCB1;"><i>$0</i></span>'</span>,
<span style="color: #0000ff;">$code</span>
<span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// hightlight braces:</span>
<span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/preg_replace"><span style="color: #000066;">preg_replace</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'/(<span style="color: #000099; font-weight: bold;">\(</span>|<span style="color: #000099; font-weight: bold;">\[</span>|<span style="color: #000099; font-weight: bold;">\{</span>|<span style="color: #000099; font-weight: bold;">\}</span>|<span style="color: #000099; font-weight: bold;">\]</span>|<span style="color: #000099; font-weight: bold;">\)</span>|<span style="color: #000099; font-weight: bold;">\-</span>>)/'</span>, <span style="color: #ff0000;">'<strong>$1</strong>'</span>, <span style="color: #0000ff;">$code</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// hightlight variables $foobar</span>
<span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/preg_replace"><span style="color: #000066;">preg_replace</span></a><span style="color: #66cc66;">(</span>
<span style="color: #ff0000;">'/(<span style="color: #000099; font-weight: bold;">\$</span>[a-zA-Z0-9_]+)/'</span>, <span style="color: #ff0000;">'<span style="color: #0000B3">$1</span>'</span>, <span style="color: #0000ff;">$code</span>
<span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">/* 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"
*/</span>
<span style="color: #808080; font-style: italic;">// special words and functions</span>
<span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/preg_replace"><span style="color: #000066;">preg_replace</span></a><span style="color: #66cc66;">(</span>
<span style="color: #ff0000;">'/<span style="color: #000099; font-weight: bold;">\b</span>(print|echo|new|function)<span style="color: #000099; font-weight: bold;">\b</span>/'</span>,
<span style="color: #ff0000;">'<span style="color: #7F007F">$1</span>'</span>, <span style="color: #0000ff;">$code</span>
<span style="color: #66cc66;">)</span>;
<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$code</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">/*example-start*/</span>
<span style="color: #808080; font-style: italic;">/*
** Create some example PHP code:
*/</span>
<span style="color: #0000ff;">$example_php_code</span> = <span style="color: #ff0000;">'
// 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;'</span>;
<span style="color: #808080; font-style: italic;">// output the formatted code:</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<pre>'</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> syntax_highlight<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$example_php_code</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'</pre>'</span>;
<span style="color: #808080; font-style: italic;">/*example-end*/</span>
以上是关于php 简单语法突出显示的主要内容,如果未能解决你的问题,请参考以下文章