PHP - Html Transfer Code
超文本轉換碼,市面上這東西已經很濫(多)了,不過是這幾天自己實踐了一下~~~~
支持多重嵌套和同標籤嵌套,混合高亮顯示(還有一點小問題)
自我感覺方便修改和擴充~~~~~~~~~~~
/****************************************************
* 🎜>* 版本: 1.3.2 *
* 建立 : 2003-09-03 *
* 修改時間: 2003-12-17 ** 首頁:無(可能很快) *
* 注意:您可自由使用和修改它, *
* 但請保留此物品。 *
* ****** *** *******/
$htc_list = array(
"B" => " => "\1" ,
"U" => "\1",n",
"URL" => "\1",
"EMAIL" " IMAGE" => "
parseInt(this.offsetParent.offsetWidth))?this.offsetParent.offsetWidth:org_w' onclick='window.open(this.src) ' style="max-width:90%" /> |
n",
"DOWNLOAD" => "exec:get_dl_html(.\ 🎜> "LIST" => "exec:get_list_html('\1')", "
n",
“IFRAME”=> "
|
n",
"移動" => "\1選框>n",
"GLOW" =>; "
n",
「陰影」 => "
n",
\1 |
n",
\1 |
n",
\1 |
n",
; "n",
src='\1' Loop='-1'>n",
"音樂" => "n",
tr>n",
"衝擊波" =>; “
; “ |
|
> ;n",
“QUICKTIME” => "
下載電影 |
n",
"REALPLAYER" => "
\n",
"MEDIAPLAYER" => "
\n",
"VOTE" => "exec:get_vote_html('\\1')",
);
$htc_list_att = array(
"ALIGN" => "
\\2
n",
"URL" => "
\ => "\2",
"影像" => "
td> "
td> '\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="max-width:90%" />
>n",
" FONTFACE " => "
\2",
"FONTSIZE" "FONTSIZE" style /span>",
"字型顏色" => "
\2 :get_dl_html('\1 ', '\2')",
"名單" => "exec:get_list_html('\2',''\1') => "exec:get_attach_html('\2 ', '\1')",
"投票" => "exec:get_vote_html('\1','\2')",
);
$htc_list_spl = array(
"PHP" => "exec:get_highlight_html('\1', 'php')", =>「執行: get_highlight_html('\1', 'css')",
"HTML" => "exec:get_highlight_html('\1', 'html c:get_highlight_html('\1', 'script')",
"代碼" =>; "exec:get_code_html('\1')",
"執行" "執行" );
function get_highlight_html($str , $type = "php") {
$type = strtolower($type);
$type_func = $type."_highlight";
result = "
";
if(!function_exists($type_func)) $type_func = "php_hightype_func. ;
$result .= "
高亮顯示{$type} 代碼: |
|
".$type_func($str)." |
".($type=="HTML"?"":"")."
)
function css_highlight($str, $nl = true) {
$color_1 = "紅色";
$color_2 = "綠色";
$color_2 = "綠色";
30 $color_2 = "綠色";
" ;
$str = str_replace("\"","",$str);
$str = str_replace("r", "", $str. \ *.w#: ] {[^{}] }/", $str, $arr_css);
$arr_css = $arr_css[0];
for($i =0; $i $head = preg_replace("/([\*.w#: ] {)[^{}] }/", " \1".($nl?"
n":""), $arr_css[$i]);
$unit = preg_replace("/[\*.ww#. ] {([^{}] )}/", "\1", $arr_css[$i]);
$unit_list = preg_split("/ unit = "n";
for($j=0; $j if(strlen($unit_list[$j])>0)
= " " . preg_replace("/s*([w-] )s*:s*(.*)/e", "'\1 : '.htmlspecialchars('\2').' '", $unit_list[$j]) 。 ($nl?"
n":"n");
}
$arr_css[$i] = $head . "}";
}
return join(($nl?"n
gt;
n " :"nn "), $arr_css);
}
function script_highlight_note($str, $mode = true) {
$color_note = "gray";
$color_note = "gray";
$color_note = "gray";
"\"",""",$str);
$str = preg_replace("/(.*)/isU ", "\ 1", $str);
$str = $mode?"/*{$str}*/":"//{$str}";
return "{ $str}";
}
function script_highlight($str, $nl = true) {
$color_1 = "紅色";
$color_2 = "藍色";
$color_3 = "棕色";
$color_4 = "綠色";
str = htmlspecialchars($str);
$str = str_replace("=","&equal;",$str);
$str = str_replace("|", "&vertical"|
$keywords = array(
"{$color_1}" "{$color_1}" "{$color_ 2}” =>佇列(“var”,“for”,“if”,“else”,“switch”,“case”,“函數”,“循環”,“繼續”,“break”,“> ” " , "!=", "==", "=", "&&", "&垂直;&垂直;"),
“{$color_3}" => array("window", "self", "this", "event", "document", "opener", "Navigator", "Math", "Array", "String", "Object", "函數", "數字"),
);
foreach($keywords as $p = $ $value ; $i $str = preg_replace("/(W)(".$value[$i].")(W)/", "\1 \; \3", $str);
}
}
$str = preg_replace("/..|字型顏色='{$color_4}'>\2(", $str);
$str = preg_replace("/.(w )(?!()/i" ,". \1", $str);
$str = preg_replace("///([^n]*)n/e" ," script_highlight_note('\ 1 ', false)", $str);
$str = preg_replace("//*(.*?)*//es" ,"script_highlight_note('\1')", $str ); > $str = str_replace("t", " ", $str);
$str = str_replace("&equal;","=",$str);
str $str = , "|",$str);
return $nl?nl2br($str):$str;
}
function html_highhigh_attr($att) { = "紅色";
$color_2 = "棕色";
$color_3 = "黑色";
$att = str_replace("\"),""
$att = preg_replace("/=s*('|")([^'"] )\1/eU","'=\1'.preg_replace('/s /','|:s :|','\2').'\1'",$att);
$att = str_replace("\"",""",$att);
$att .= " " ;
preg_match_all("/[a-z:/-] (=(['"]?).*[^\\]{1}\2?)?s /iU", $att, $att_list);
$att_list = $att_list[0];
for($i=0; $i $att_list[$i] = trim($att_list[$i]);
$🎜> $(h._list) $(/place)(Iplace)( '"]?)(.*)(\3)/", "\ 1字型>\2字型>\3\4 字型>\3", $att_list [$i]);
}
return str_replace("|:s:|",|:ss:| " ", join(" ", $att_list));
}
function html_highlight($str) {
$color_1 = " 紅色";
$color_1 = " 紅色";
$color_1 = " 紅色";
藍色";
$color_3 = "灰色";
$str = str_replace("\"", """,$str);
> )>(.*)/isU", $str, $arr_tmp);
$arr_script = $arr_tmp[0];
for($i=0; $i $str = str_replace($arr_script[$i], "
::script_{$i}::
", $str);
::script_{$i}::]*)>(.* )/iseU", "'<script><br />'.script_highlight(str_replace('\"','"','\2'), false).'<font color='{$color_2}'></ SCRIPT>; '", $arr_script[$i]);<br> }<br> $str = preg_replace("/(<textarea[^>]*>)(.*)()/即U" ,"'\1'.htmlspecialchars('\2').'\3'", $str);<br> $str = preg_replace("/<!--(.*)- -> /seU" ,"'<!--'.htmlspecialchars('\1').'-->'", $str);<br> $str = preg_replace("/<style([^>gt ) ) ;]*)>(.*)/isU" ,"<!style\1>\2!>", $str);<br> $str = preg_replace(" /< ( [a-z:] )s*(.*?)( /|/)?>/ise" ,"'< 字型顏色='{$color_2}'><\1 '.html_highlight_attr('\2').'\3></ font>'", $str);<br> $str = preg_replace("/<(/[a-z: ] )>/ i" ,"<字體顏色='{$color_2}'><\1>", $str);<br> $str = preg_replace("/<; (!--.*--) >/sU" ,"<字體顏色='{$color_3}'><\1>", $str);<br> $str = preg_replace("/<!style([^>]* )>(.*)!>/iseU" ,"'<字體顏色='{$color_2}'>< ;樣式'.html_highlight_attr('\1').'><br / >'.css_highlight(str_replace('\"','"','\2'), false)。 ", $str);<br> for($i=0; $i<; count($arr_script); $i ) {<br> $str = str_replace("<br>} :<br>", $arr_script[$i], $str);<br> }<br> $str = str_replace("</ font>", "", $str);<br> $str = str_replace("</ b>", " ;", $str);<br> $str = str_replace(" >", ">", $str);<br> $str = str_replace(" "," ",$str);<br> $str = str_replace("t"," ",$str);<br> return nl2br($str);<br> ><br>function get_file_pic($filename) {<br> 全域$root_path;<br> $ext = str_replace(" ),strrchr($filename,".) ); $root_path."images/mime_type/{$ext}.gif" ;<br> return file_exists($pic)?"$pic":$root_path."images/mime_type/attch.gif";<br>$root_path."images/mime_type/attch.gif";<br>}<br>}<br>}< ><br><br>function get_dl_html($filename, $str = "") {<br> return "<img src='".get_file_pic($filename)."' border='0' alt="PHP - Html Transfer Code_PHP教程" >; <a href='$filename'>".(empty($str)?basename($filename):$str)."";<br>}<br><br><br>函數 get_attach_html ($filename, $id) {<br> return "<img src='".get_file_pic($filename)."' border='0' alt="PHP - Html Transfer Code_PHP教程" >; <a href='attach_manage.php?method=download&id=$id'>$filename";<br>}<br><br><br>function get_vote_html($vote_idx, $message = " ") {<br> 全域$db_host, $db_user, $db_pass, $db_name;<br> $db = new DB_Manager($db_host, $db_user, $db_pass);< >SelectDB($db_name);<br> $db->Query("select * from mnms_poll where id=".$vote_idx);<br> $record = $db->Gstret ()<db-> "";<br> $db->close();<br> unset($db);<br> if($record) {<br> if($record) {<br> if($record) {<br> $if_vote = !( strpos($record[vote_user], "|{$username}|")===false) <br> strpos($record[vote_user], "|".GetIp() ."|") > str .= "<br> <fieldset><br> he vote_title ] :$訊息)。 🎜> < 表單名稱='poll' action='poll_manage.php? <表格寬度='90年%'align='center'>";<br> $ dote_list = split(“ ::”,$ record [fote_list]); <br> $ potter_count = split(“ ::”,$ record [dovel_count]); <<br> $ potture_users = count(split(split(“ ”) n", $record[vote_user])) - 1;<br> $vote_sum = array_sum($vote_count);<> str < tr><td colspan='2'> 目前共有 <字體顏色='紅色'>{$vote_users}</字體> 人參與了這次投票";<br> if($vote_sum==0) $vote_sum = 1;<br> ==0) $vote_sum = 1;<br> $vote_list) ); $i ) {<br> $str .= "<br> "► ":"<輸入類型='隱藏'值='".$ vote_count[$i]."' name='vote_count[]'><輸入型別='".($record[vote_multi]=='Y'?"checkbox":"radio")."' name= 'vote []' value='{$i}'>").htmlspecialchars($vote_list[$i])."<td width='400'><img src='圖片/條/條". ($i 1).".gif' style="max-width:90%".ceil($vote_count[$i]*300/$vote_sum)."' style="max-width:90%" alt="PHP - Html Transfer Code_PHP教程" > 佔總票數的<font color='#990000' >".ceil((int)$vote_count[$i]*100/$vote_sum)."% tr>n";<br> }<🎜> }<🎜> . <tr><td colspan='2'align='center'><br>".($if_vote?"<b> ;您已經投過票了!":"<input type='hidden' value='{$record[id]}' name='id'><input type='Submit' value='確定' name= '提交'> < input type='reset' value='重置' name='reset'>")."";<br> $str ";<br> }<br> return $str< $str = str_replace("\"" ,""",$str);<br> return "<br> <表格對齊='center' width='550'><br> 相信 10' name=Code>".htmlspecialchars($str)."<br><br> <輸入型別= 'button' value='執行碼' onClick="str=this.parentNode.firstd. ;code_win=window.open('about:blank');doc=code_win.document;doc.open();doc.write (str);doc.close();code_win.focus();"><br> <input type='button' value='複製程式碼' onclick="try{window.clipboardData.setData('text' ,this.parentNode.firstChild.value);alert('將Codez 儲存到剪貼簿!'); }catch(e){alert('請複製選定的Codez!');this.parentNode.firstChild.select(); }"><br> <input type='button' value='儲存程式碼' onClick='儲存程式碼' onC. "str=this.parentNode.firstChild.value;code_win=window.open('about:blank','_blank', 'top=10000');code_win.document.writeln(str);code_win.document.execCommand(' saveas','','code.html');code_win.close();"><br> (提示:先修改部分代碼)<br> >";<br>}<br><br><br>function get_list_html($content, $ type = "") {<br> $content = preg_replace("/(<br />)?[rn] />)?[rn] /> ", "n", $content);<br> $content = preg_replace("/[ rn] /", "n", $content);<br> $content = preg_replace("/^[n]*( .*)[n]*$/m", "\1", $content); <br> $content = str_replace("n", "<li>", $content);<br> $content = "<ul type='$type'><li>; $contentn";<br> return $content;<br>}<br><br><br> function html_trans($str) {<br> $search = array(" &", "'", ", ">", " ", "t");<br> $替換= 數組("&", "'", """, " <", ">", " ", ">", (str_replace($search, $replace, $str));<br> }<br><br><br>function link_url ($str) {<br> $str = preg_replace("/((http|ftp|mms|rtsp|pnm|mail)://[w@:/ ?=&;#-%] )/i" , "<a href='\1' target='_blank'>\1", $str);<br> $str = preg_replace("/ (w @(w .) [w] {2,3})/i", "<a href='mailto:\1'>\1", $str);<br> $str = preg_replace("/(href|src) )='<a href=('. ')>[^<] '/iU", "\1=\2", $str);<br> return $str;<br>}<br><br><br>function htc2html_loop($str) {<br> 全域$htc_list, $htc_list_att;< > 線上$ ) for $ <br> if(substr($value,0,5)= ="exec:"){<br> /$ ?([^]] ))?]))*)[ /$key]/iexsU", substr($value,5), $str);<br> }else {<br> $str = preg_replace("/[$key]((.(?![$key(s?=s?([^]] )))? /ixsU", "$value", $str);<br> }<br> }<br> if(substr($value,0, 5 ) )=="exec:"){<br> $str = preg_replace("/[$keys?=s?([^]] )](.(.(?!)(s? [ ^]] ))?]))*)[/$key]/iesxU", substr($value,5), $str);<br> } "/[ $鍵? " $value", $str);<br> }<br> }<br> return $str. , $loop = 3) {<br> 全域$htc_list_spl;<br> $str = preg_replace("/([w ])s*/ixs","\1",$str);<br>< / s*([/w ])/ixs","\1",$str);<br> foreach($htc_list_spl as $key => $value) {<br> => $value) {<br> . *)[/$key]/ixsU", $str, $arr_tmp);<br> $arr_spl[$key] = $arr_tmp[0];<br> $arr_spl[$key] = $arr_tmp[0];<br> $arr_spl[$key] = $arr_tmp[0] $i<count($arr_spl[$key]); $i ) {<br> $str = str_replace($arr_spl[$key][$i], "<br>::{$key}_{$n} 🎜> if(substr($value,0,5)=="exec:"){<br> key][$i] = preg_replace("/[$key](.*)[/$ key]/iexsU", substr($value,5), $arr_spl[$key][$i]);<br> $arr_spl[$key][$i] = preg_re地點(“ / [$ key](。*)[/$ key]/ixsu”,“ $ value”,$ arr_spl [$ key] [$ i] [$ i]); <br>} <br>} <br>} str = html_trans($str);<br> while($loop-- > 0) {<br> while =s?([^]] ))? ](.*)[/\1]/isU", $str)) Break;<br> $str = htc2html_loop($str);<br> $str = htc2html_loop($str);<br> if($trans_url) $str = link_url($ str);<br> foreach($htc_list_spl as $key => $value) {<br> ”$$ arr arr for$($ for $. ) ]); $i ) {<br> $str = str_replace("<br>::{$key}_{$i}::<br>", $arr_spl[$key][$i], $ str );<br> }<br> } <br> 使用<br>echo htc2html ( $str);<br><br>好啦~~~~~~~~~<br>
<br>
<br><br><br>http://www.bkjia.com/PHPjc/314366.html<br><br>www.bkjia.com<br><br>true<br><br>http: //www.bkjia.com/PHPjc/314366.html<br><br>TechArticle<br><br>PHP - Html 轉換傳輸程式碼超文本碼,讀這東西已經很豐富了,不過是這幾天自己實踐交流~~~~支援電壓喚醒和同標籤靜音、混合...<br><p align="left"><div style="display:none;">
<span id="url" itemprop="url"></script>