highlight_string
Description
The highlight_string of Miscellaneous for PHP syntax highlighting of a string.
Syntax
highlight_string( string $string, bool $return = false ): string|true
Parameters
string
The PHP code to be highlighted. This should include the opening tag.
return
Set this parameter to true to make this function return the highlighted code.
Return
If return is set to true, returns the highlighted code as a string instead of printing it out. Otherwise, it will return true.
Examples
1 · string
<? $string = '<? // comment function myfunction() { $myvariable = "hello"; echo $myvariable; } myfunction();'; highlight_string($string);
<pre><code style="color: #000000"><span style="color: #0000BB"><? </span><span style="color: #FF8000">// comment </span><span style="color: #007700">function </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">() { </span><span style="color: #0000BB">$myvariable </span><span style="color: #007700">= </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">; echo </span><span style="color: #0000BB">$myvariable</span><span style="color: #007700">; } </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">();</span></code></pre>
2 · return · false
<? $string = '<? // comment function myfunction() { $myvariable = "hello"; echo $myvariable; } myfunction();'; $return = false; highlight_string($string, $return);
<pre><code style="color: #000000"><span style="color: #0000BB"><? </span><span style="color: #FF8000">// comment </span><span style="color: #007700">function </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">() { </span><span style="color: #0000BB">$myvariable </span><span style="color: #007700">= </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">; echo </span><span style="color: #0000BB">$myvariable</span><span style="color: #007700">; } </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">();</span></code></pre>
3 · return · true
<? $string = '<? // comment function myfunction() { $myvariable = "hello"; echo $myvariable; } myfunction();'; $return = true; highlight_string($string, $return);
4 · output · return · false
<? $string = '<? // comment function myfunction() { $myvariable = "hello"; echo $myvariable; } myfunction();'; $return = false; $output = highlight_string($string, $return); var_export($output);
<pre><code style="color: #000000"><span style="color: #0000BB"><? </span><span style="color: #FF8000">// comment </span><span style="color: #007700">function </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">() { </span><span style="color: #0000BB">$myvariable </span><span style="color: #007700">= </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">; echo </span><span style="color: #0000BB">$myvariable</span><span style="color: #007700">; } </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">();</span></code></pre>true
5 · output · return · true
<? $string = '<? // comment function myfunction() { $myvariable = "hello"; echo $myvariable; } myfunction();'; $return = true; $output = highlight_string($string, $return); echo $output;
<pre><code style="color: #000000"><span style="color: #0000BB"><? </span><span style="color: #FF8000">// comment </span><span style="color: #007700">function </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">() { </span><span style="color: #0000BB">$myvariable </span><span style="color: #007700">= </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">; echo </span><span style="color: #0000BB">$myvariable</span><span style="color: #007700">; } </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">();</span></code></pre>
6 · ini_set
<? ini_set("highlight.comment", "#ffff00"); ini_set("highlight.default", "#0000ff"); ini_set("highlight.html", "#000000"); ini_set("highlight.keyword", "#00ff00"); ini_set("highlight.string", "#ff0000"); $string = '<? // comment function myfunction() { $myvariable = "hello"; echo $myvariable; } myfunction();'; highlight_string($string);
<pre><code style="color: #000000"><span style="color: #0000ff"><? </span><span style="color: #ffff00">// comment </span><span style="color: #00ff00">function </span><span style="color: #0000ff">myfunction</span><span style="color: #00ff00">() { </span><span style="color: #0000ff">$myvariable </span><span style="color: #00ff00">= </span><span style="color: #ff0000">"hello"</span><span style="color: #00ff00">; echo </span><span style="color: #0000ff">$myvariable</span><span style="color: #00ff00">; } </span><span style="color: #0000ff">myfunction</span><span style="color: #00ff00">();</span></code></pre>