[摘要]PHP - Html Transfer Code超文本转换码,市面上这东西已经很滥(多)了,不过是这几天自己实践了一下~~~~支持 多重嵌套 和 同标签嵌套,混合高亮显示(还有一点小问题)自我感觉便...
PHP - Html Transfer Code超文本转换码,市面上这东西已经很滥(多)了,不过是这几天自己实践了一下~~~~
支持 多重嵌套 和 同标签嵌套,混合高亮显示(还有一点小问题)
自我感觉便于修改和扩充~~~~~~~~~~~
<?php
/**************************************************
* *
* Name : Functions 4 Html Transfer Code (HTC) *
* Version : 1.3.2 *
* Author : Windy_sk *
* Create : 2003-09-03 *
* Modified: 2003-12-17 *
* Email : windy_sk@126.com *
* HomePage: None (Maybe Soon) *
* Notice : U Can Use & Modify it freely, *
* BUT PLEASE HOLD THIS ITEM. *
* *
**************************************************/
$htc_list = array(
"B" => "<b>\\1</b>",
"I" => "<i>\\1</i>",
"U" => "<u>\\1</u>",
"CENTER" => "<center>\\1</center>\n",
"URL" => "<a href='\\1'>\\1</a>",
"EMAIL" => "<a href='mailto:\\1'>\\1</a>",
"IMAGE" => "<br /><table align=center width='100%'><tr><td align='center'><img src='\\1' border='0' onload='org_w=this.offsetWidth;this.style.width=10;this.style.width=(org_w>parseInt(this.offsetParent.offsetWidth))?this.offsetParent.offsetWidth:org_w' onclick='window.open(this.src)' style='cursor:pointer' /></td></tr></table><br />\n",
"DOWNLOAD" => "exec:get_dl_html('\\1')",
"LIST" => "exec:get_list_html('\\1')",
"QUOTE" => "<center><table border='0' width='90%' cellspacing='0' cellpadding='0'><tr><td> 引用:</td></tr><tr><td><table width='100%' class='quote'><tr><td>\\1</td></tr></table></td></tr></table></center>\n",
"IFRAME" => "<table align=center><tr><td><iframe src='\\1' frameborder='0' allowtransparency='true' scrolling='yes' width='400' height='300'></iframe></td></tr></table>\n",
"MOVE" => "<center><marquee width='90%' behavior='alternate' scrollamount='3'>\\1</marquee><center>\n",
"GLOW" => "<table><tr><td class='glow'>\\1</td></tr></table>\n",
"SHADOW" => "<table><tr><td class='shadow'>\\1</td></tr></table>\n",
"DROPSHADOW" => "<table><tr><td class='dropshadow'>\\1</td></tr></table>\n",
"GBMUSIC" => "<bgsound src='\\1' loop='-1'>\n",
"MUSIC" => "<TABLE cellSpacing=0 cellPadding=0 align=center border=0><TBODY><TR><TD vAlign=top colSpan=3><EMBED name=music_rm align=top src='\\1' width='200' height='30' type='audio/x-pn-realaudio-plugin' console='ClipNN' loop='false' autostart='false' controls='ControlPanel' border='1' vspace='0' hspace='0'></EMBED><br><STRONG><A href='\\1'>Download The Music</A></STRONG></TD></TR></TBODY></TABLE>\n",
"FLASH" => "<table align=center><tr><td><object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width='400'><param name='movie' value='\\1'><param name='quality' value='high'><embed src='\\1' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='32' height='32'></embed></object></td></tr></table>\n",
"SHOCKWAVE" => "<table align=center><tr><td><object classid='clsid:166B1BCA-3F9C-11CF-8075-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,0,0' width='400'><param name='src' value='\\1'><embed src='\\1' pluginspage='http://www.macromedia.com/shockwave/download/' width='400'></embed></object></td></tr></table>\n",
"QUICKTIME" => "<table align=center><tr><td><embed src='\\1' width='400' height='300' autoplay='true' loop='false' controller='true' playeveryframe='false' cache='false' scale='TOFIT' bgcolor='#000000' kioskmode='false' targetcache='false' pluginspage='http://www.apple.com/quicktime/'><br><STRONG><A href='\\1'>Download The Movie</A></STRONG></td></tr></table>\n",
"REALPLAYER" => "<TABLE cellSpacing=0 cellPadding=0 align=center border=0><TBODY><TR><TD vAlign=top colSpan=3><EMBED name=movie_rm align=top src='\\1' width='400' height='300' type='audio/x-pn-realaudio-plugin' console='ClipNN' loop='true' autostart='true' controls='ImageWindow,ControlPanel' border='1' vspace='0' hspace='0'></EMBED><br><STRONG><A href='\\1'>Download The Movie</A></STRONG></TD></TR></TBODY></TABLE>\n",
"MEDIAPLAYER" => "<table align=center><tr><td><object classid='clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95' width='400'><param name='ShowStatusBar' value='-1'><param name='Filename' value='\\1'><embed type='application/x-oleobject' codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701' flename='mp' src='\\1' width='400'></embed></object><br /><STRONG><A href='\\1'>Download The Movie</A></STRONG></td></tr></table>\n",
"VOTE" => "exec:get_vote_html('\\1')",
);
$htc_list_att = array(
"ALIGN" => "<div align='\\1'>\\2</div>\n",
"URL" => "<a href='\\1'>\\2</a>",
"EMAIL" => "<a href='mailto:\\1'>\\2</a>",
"IMAGE" => "<br /><table align=center width='100%'><tr><td><img alt='\\1' src='\\2' border='0' onload='org_w=this.offsetWidth;this.style.width=10;this.style.width=(org_w>parseInt(this.offsetParent.offsetWidth))?this.offsetParent.offsetWidth:org_w;' onclick='window.open(this.src)' style='cursor:pointer' /></td></tr></table><br />\n",
"FONTFACE" => "<span style='FONT-FAMILY:\\1'>\\2</span>",
"FONTSIZE" => "<span style='font-size:\\1'>\\2</span>",
"FONTCOLOR" => "<span style='color:\\1'>\\2</span>",
"DOWNLOAD" => "exec:get_dl_html('\\1', '\\2')",
"LIST" => "exec:get_list_html('\\2','\\1')",
"ATTACH" => "exec:get_attach_html('\\2', '\\1')",
"VOTE" => "exec:get_vote_html('\\1','\\2')",
);
$htc_list_spl = array(
"PHP" => "exec:get_highlight_html('\\1', 'php')",
"CSS" => "exec:get_highlight_html('\\1', 'css')",
"HTML" => "exec:get_highlight_html('\\1', 'html')",
"SCRIPT" => "exec:get_highlight_html('\\1', 'script')",
"CODE" => "exec:get_code_html('\\1')",
"EXECUTE" => "\\1",
);
function get_highlight_html($str, $type = "php") {
$type = strtolower($type);
$type_func = $type."_highlight";
$type = strtoupper($type);
$result = "<center><table border='0' width='90%' cellspacing='0' cellpadding='0'>";
if(!function_exists($type_func)) $type_func = "php_highlight";
$result .= "
<tr><td>高亮显示 <font color='red'>{$type}</font> 代码:</td></tr>
<tr><td>
<table width='100%' class='quote'><tr><td>
".$type_func($str)."
</td></tr></table>
".($type=="HTML"?"<input type='button' value='运行代码' onclick=\"str=this.parentNode.firstChild.innerText;if(str==undefined){alert('Execute Codez Failed !');return;}code_win=window.open('about:blank');doc=code_win.document;doc.open();doc.write(str);doc.close();code_win.focus();\" />":"")."
<input type='button' value='复制代码' onclick=\"try{window.clipboardData.setData('text',this.parentNode.firstChild.innerText);alert('Save Codez To The Clipboard !');}catch(e){alert('Codez Copy Failed !');}\">
<input type='button' value='保存代码' onClick=\"try{str=this.parentNode.firstChild.innerText;code_win=window.open('about:blank','_blank','top=10000');code_win.document.writeln(str);code_win.document.execCommand('saveas','','code.html');code_win.close();}catch(e){alert('Codez Save Failed !');}\">
</td></tr>
</table></center>
";
return $result;
}
function php_highlight($str) {
$str = str_replace("\\\"","\"",$str);
return highlight_string($str, true);
}
function css_highlight($str, $nl = true) {
$color_1 = "red";
$color_2 = "green";
$color_3 = "blue";
$str = str_replace("\\\"","\"",$str);
$str = str_replace("\r", "", $str);
preg_match_all("/[\\\*\.\w#: ]+\{[^\{\}]+\}/", $str, $arr_css);
$arr_css = $arr_css[0];
for($i=0; $i<count($arr_css); $i++) {
$head = preg_replace("/([\\\*\.\w#: ]+\{)[^\{\}]+\}/", "<font color='{$color_1}'>\\1</font>".($nl?"<br />\n":""), $arr_css[$i]);
$unit = preg_replace("/[\\\*\.\w#: ]+\{([^\{\}]+)\}/", "\\1", $arr_css[$i]);
$unit_list = preg_split("/;\s*/", $unit);
$unit = "\n";
for($j=0; $j<count($unit_list); $j++) {
if(strlen($unit_list[$j])>0)
$unit .= " " . preg_replace("/\s*([\w\-]+)\s*:\s*(.*)/e", "'<font color=\'{$color_2}\'>\\1</font> : <font color=\'{$color_3}\'>'.htmlspecialchars('\\2').'</font> ; '", $unit_list[$j]) . ($nl?"<br />\n":"\n");
}
$arr_css[$i] = $head . $unit . "<font color='{$color_1}'>}</font>";
}
return join(($nl?"\n<br /><br />\n":"\n\n"), $arr_css);
}
function script_highlight_note($str, $mode = true) {
$color_note = "gray";
$str = str_replace("\\\"","\"",$str);
$str = preg_replace("/<font color='[a-z]+'>(.*)<\/font>/isU", "\\1", $str);
$str = $mode?"/*{$str}*/":"//{$str}";
return "<font color='{$color_note}'>{$str}</font>";
}
function script_highlight($str, $nl = true) {
$color_1 = "red";
$color_2 = "blue";
$color_3 = "brown";
$color_4 = "green";
$str = str_replace("\\\"","\"",$str);
$str = htmlspecialchars($str);
$str = str_replace("=","&equal;",$str);
$str = str_replace("
关键词:PHP - Html Transfer Code