PHP highlight in Javascript
Javascript
<script>
function h_js(name) {
h_base(name, "js_", "(function|var|null|false|true|try|catch|if|else|new|return|for)", "([\!\(\)=\}\{,.])");
}
function h_php(name) {
h_base(name, "php_", "(function|global|array|echo|if|else|elseif|return|foreach|as|[\!\(\)=\}\{,.])");
}
function h_java(name) {
h_base(name, "java_", "(package|import|public|private|static|final|class|void|null|boolean|int|false|true|try|catch|if|else|new|return|for)");
}
function h_base(name, wpre, bvar, bres) {
obj=document.getElementById(name); if (!obj) return;
bvar=new RegExp(bvar, "ig");
bquote=new RegExp("(\"[^\"]+\"|'[^']+')", "ig");
bcomm=new RegExp("(\/\/ .*)", "ig");
if (bres) bres=new RegExp(bres, "ig");
ta=obj.innerHTML.split(lnbr);
for (i=0; i < ta.length; i++) {
cl=wpre+"res"; if (bres) ta[i]=ta[i].replace(bres, "<font class="+cl+">$1</font>");
cl=wpre+"var"; ta[i]=ta[i].replace(bvar, "<font class="+cl+">$1</font>");
cl=wpre+"quote"; ta[i]=ta[i].replace(bquote, "<font class="+cl+">$1</font>");
cl=wpre+"comment"; ta[i]=ta[i].replace(bcomm, "<font class="+cl+">$1</font>");
}
tj=ta.join("_LN_"); cl=wpre+"comment"; tj=tj.replace(/(\/\*.*?\*\/)/gi, "<font class="+cl+">$1</font>"); ta=tj.split("_LN_");
cl=wpre+"in"; obj.innerHTML="<pre class="+cl+">"+ta.join("\n")+"</pre>";
}
</script>
change code to push it to blog
function out_html($fname) {
$tt=htmlentities(file_get_contents($fname));
$tt=str_replace("\t", " ", $tt);
echo $tt;
}
function js_esc($fname) {
$txt=file_get_contents('1.txt');
$txt=preg_replace('/\r?\n/', '', $txt);
$txt=str_replace('\\', '\\\\', $txt);
$txt=str_replace('"', '\\"', $txt);
echo "<script>document.write(escape(\"$txt\"));</script>";
}
No comments:
Post a Comment