HighLight کردن کدهای HTML درون متن :
کد PHP:
<?php
function highlight_html($string, $decode = TRUE){
$tag = '#0000ff';
$att = '#ff0000';
$val = '#8000ff';
$com = '#34803a';
$find = array(
'~(\s[a-z].*?=)~', // Highlight the attributes
'~(<\!--.*?-->)~s', // Hightlight comments
'~("[a-zA-Z0-9\/].*?")~', // Highlight the values
'~(<[a-z].*?>)~', // Highlight the beginning of the opening tag
'~(</[a-z].*?>)~', // Highlight the closing tag
'~(&.*?;)~', // Stylize HTML entities
);
$replace = array(
'<span style="color:'.$att.';">$1</span>',
'<span style="color:'.$com.';">$1</span>',
'<span style="color:'.$val.';">$1</span>',
'<span style="color:'.$tag.';">$1</span>',
'<span style="color:'.$tag.';">$1</span>',
'<span style="font-style:italic;">$1</span>',
);
if($decode)
$string = htmlentities($string);
return '<pre>'.preg_replace($find, $replace, $string).'</pre>';
}
echo highlight_html('
<!-- This is an
HTML comment -->
<a href="home.html" style="color:blue;">Home</a>
<p>Go & here.</p>
<!-- This is an HTML comment -->
<form action="/login.php" method="post">
<input type="text" value="User Name" />
</form>
');
?>