Menu Home

highlight_string

osbo.com | PHP | Functions | highlight_string

Syntax highlighting of a string

Syntax

highlight_string ( string $str [, bool $return = FALSE ] ) : mixed

Parameters

str

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 on success, FALSE on failure.

Examples

str

<?

$str = '<?

// comment
function myfunction()
{
    $myvariable = "Hello";

    echo $myvariable;
}

myfunction();

?>';

highlight_string($str);

?>
<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?
<br />
<br /></span><span style="color: #FF8000">//&nbsp;comment
<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">myfunction</span><span style="color: #007700">()
<br />{
<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$myvariable&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Hello"</span><span style="color: #007700">;
<br />
<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$myvariable</span><span style="color: #007700">;
<br />}
<br />
<br /></span><span style="color: #0000BB">myfunction</span><span style="color: #007700">();
<br />
<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code>

return

<?

$str = '<?

// comment
function myfunction()
{
    $myvariable = "Hello";

    echo $myvariable;
}

myfunction();

?>';
$return = true;

$result = highlight_string($str, $return);
echo $result;

?>
<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?
<br />
<br /></span><span style="color: #FF8000">//&nbsp;comment
<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">myfunction</span><span style="color: #007700">()
<br />{
<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$myvariable&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Hello"</span><span style="color: #007700">;
<br />
<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$myvariable</span><span style="color: #007700">;
<br />}
<br />
<br /></span><span style="color: #0000BB">myfunction</span><span style="color: #007700">();
<br />
<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code>

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');

$str = '<?

// comment
function myfunction()
{
    $myvariable = "Hello";

    echo $myvariable;
}

myfunction();

?>';

highlight_string($str);

?>
<code><span style="color: #000000">
<span style="color: #0000ff">&lt;?
<br />
<br /></span><span style="color: #ffff00">//&nbsp;comment
<br /></span><span style="color: #00ff00">function&nbsp;</span><span style="color: #0000ff">myfunction</span><span style="color: #00ff00">()
<br />{
<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">$myvariable&nbsp;</span><span style="color: #00ff00">=&nbsp;</span><span style="color: #ff0000">"Hello"</span><span style="color: #00ff00">;
<br />
<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000ff">$myvariable</span><span style="color: #00ff00">;
<br />}
<br />
<br /></span><span style="color: #0000ff">myfunction</span><span style="color: #00ff00">();
<br />
<br /></span><span style="color: #0000ff">?&gt;</span>
</span>
</code>

PHP