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

1 · 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>

2 · 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>

3 · 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>

__halt_compiler

connection_aborted

connection_status

constant

define

defined

die

eval

exit

highlight_file

hrtime

ignore_user_abort

pack

php_strip_whitespace

show_source

sleep

sys_getloadavg

time_nanosleep

time_sleep_until

uniqid

unpack

usleep

PHP

Home Menu