php 简单语法突出显示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 简单语法突出显示相关的知识,希望对你有一定的参考价值。

<span style="color: #000000; font-weight: bold;">function</span> syntax_highlight<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// this matches --&gt; &quot;foobar&quot; &lt;--</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;">&#40;</span>
        <span style="color: #ff0000;">'/&quot;(.*?)&quot;/U'</span>, 
        <span style="color: #ff0000;">'&amp;quot;&lt;span style=&quot;color: #007F00&quot;&gt;$1&lt;/span&gt;&amp;quot;'</span>, <span style="color: #0000ff;">$code</span>
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// hightlight functions and other structures like --&gt; function foobar() &lt;--- </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;">&#40;</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&lt;span style=&quot;color: #0000ff&quot;&gt;$2&lt;/span&gt;$3'</span>, 
        <span style="color: #0000ff;">$code</span>
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <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;">&#40;</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;">'&lt;span style=&quot;color: #660066; background-color: #FFFCB1;&quot;&gt;&lt;i&gt;$0&lt;/i&gt;&lt;/span&gt;'</span>, 
        <span style="color: #0000ff;">$code</span>
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <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;">&#40;</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;">'&lt;span style=&quot;color: #660066; background-color: #FFFCB1;&quot;&gt;&lt;i&gt;$0&lt;/i&gt;&lt;/span&gt;'</span>, 
        <span style="color: #0000ff;">$code</span>
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <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;">&#40;</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>&gt;)/'</span>, <span style="color: #ff0000;">'&lt;strong&gt;$1&lt;/strong&gt;'</span>, <span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <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;">&#40;</span>
        <span style="color: #ff0000;">'/(<span style="color: #000099; font-weight: bold;">\$</span>[a-zA-Z0-9_]+)/'</span>, <span style="color: #ff0000;">'&lt;span style=&quot;color: #0000B3&quot;&gt;$1&lt;/span&gt;'</span>, <span style="color: #0000ff;">$code</span>
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* The \b in the pattern indicates a word boundary, so only the distinct
    ** word &quot;web&quot; is matched, and not a word partial like &quot;webbing&quot; or &quot;cobweb&quot; 
    */</span>
&nbsp;
    <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;">&#40;</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;">'&lt;span style=&quot;color: #7F007F&quot;&gt;$1&lt;/span&gt;'</span>, <span style="color: #0000ff;">$code</span>
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$code</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/*example-start*/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*
** Create some example PHP code:
*/</span>
&nbsp;
<span style="color: #0000ff;">$example_php_code</span> = <span style="color: #ff0000;">'
// some code comment:
$example = &quot;foobar&quot;;
&nbsp;
print $_SERVER[&quot;REMOTE_ADDR&quot;];
&nbsp;
$array = array(1, 2, 3, 4, 5);
&nbsp;
function example_function($str) {
    // reverse string
    echo strrev($obj);
}
&nbsp;
print example_function(&quot;foo&quot;);
&nbsp;
/*
** A multiple line comment
*/
&nbsp;
print &quot;Something: &quot; . $example;'</span>;
&nbsp;
&nbsp;
<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;">'&lt;pre&gt;'</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> syntax_highlight<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$example_php_code</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'&lt;/pre&gt;'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/*example-end*/</span>

以上是关于php 简单语法突出显示的主要内容,如果未能解决你的问题,请参考以下文章

Sublime 3 编辑器语法突出显示 PHP + HTML 不起作用

更改 netbeans 中单个文件的语法突出显示

HTML 令人敬畏的代码语法突出显示变得简单

PyCharm - 社区版是不是能够突出显示 php?

PHP 简单的PHP搜索,包括全字提取和突出显示的搜索词

使用 Notepad++ 在 HTML 文件中突出显示 EJS 语法