PHP头条
热点:

PHP-HtmlTransferCode


PHP - Html Transfer Code 超文本转换码,市面上这东西已经很滥(多)了,不过是这几天自己实践了一下~~~~

支持 多重嵌套 和 同标签嵌套,混合高亮显示(还有一点小问题)

自我感觉便于修改和扩充~~~~~~~~~~~

/**************************************************
* *
* 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" => "\https://www.php1.cn/detail/\1",
"I" => "\https://www.php1.cn/detail/\1",
"U" => "\https://www.php1.cn/detail/\1",
"CENTER" => "
\https://www.php1.cn/detail/\1
\n",
"URL" => "\https://www.php1.cn/detail/\1",
"EMAIL" => "\https://www.php1.cn/detail/\1",
"IMAGE" => "

\n",
"DOWNLOAD" => "exec:get_dl_html('\https://www.php1.cn/detail/\1')",
"LIST" => "exec:get_list_html('\https://www.php1.cn/detail/\1')",
"QUOTE" => "
  引用:
\https://www.php1.cn/detail/\1
\n",
"IFRAME" => "
\n",
"MOVE" => "
\https://www.php1.cn/detail/\1
\n",
"GLOW" => "
\https://www.php1.cn/detail/\1
\n",
"SHADOW" => "
\https://www.php1.cn/detail/\1
\n",
"DROPSHADOW" => "
\https://www.php1.cn/detail/\1
\n",
"GBMUSIC" => "\n",
"MUSIC" => "

Download The Music
\n",
"FLASH" => "
\n",
"SHOCKWAVE" => "
\n",
"QUICKTIME" => "

Download The Movie
\n",
"REALPLAYER" => "

Download The Movie
\n",
"MEDIAPLAYER" => "

Download The Movie
\n",
"VOTE" => "exec:get_vote_html('\https://www.php1.cn/detail/\1')",
);

$htc_list_att = array(
"ALIGN" => "

\https://www.php1.cn/detail/\2

\n",
"URL" => "\https://www.php1.cn/detail/\2",
"EMAIL" => "\https://www.php1.cn/detail/\2",
"IMAGE" => "
\https://www.php1.cn/detail/\1

\n",
"FONTFACE" => "\https://www.php1.cn/detail/\2",
"FONTSIZE" => "\https://www.php1.cn/detail/\2",
"FONTCOLOR" => "\https://www.php1.cn/detail/\2",
"DOWNLOAD" => "exec:get_dl_html('\https://www.php1.cn/detail/\1', '\https://www.php1.cn/detail/\2')",
"LIST" => "exec:get_list_html('\https://www.php1.cn/detail/\2','\https://www.php1.cn/detail/\1')",
"ATTACH" => "exec:get_attach_html('\https://www.php1.cn/detail/\2', '\https://www.php1.cn/detail/\1')",
"VOTE" => "exec:get_vote_html('\https://www.php1.cn/detail/\1','\https://www.php1.cn/detail/\2')",
);

$htc_list_spl = array(
"PHP" => "exec:get_highlight_html('\https://www.php1.cn/detail/\1', 'php')",
"CSS" => "exec:get_highlight_html('\https://www.php1.cn/detail/\1', 'css')",
"HTML" => "exec:get_highlight_html('\https://www.php1.cn/detail/\1', 'html')",
"SCRIPT" => "exec:get_highlight_html('\https://www.php1.cn/detail/\1', 'script')",
"CODE" => "exec:get_code_html('\https://www.php1.cn/detail/\1')",
"EXECUTE" => "\https://www.php1.cn/detail/\1",
);


function get_highlight_html($str, $type = "php") {
$type = strtolower($type);
$type_func = $type."_highlight";
$type = strtoupper($type);
$result = "
";
if(!function_exists($type_func)) $type_func = "php_highlight";
$result .= "


高亮显示 {$type} 代码:


".$type_func($str)."

".($type=="HTML"?"":"")."



";
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 $head = preg_replace("/([\\\*\.\w#: ]+\{)[^\{\}]+\}/", "\https://www.php1.cn/detail/\1".($nl?"
\n":""), $arr_css[$i]);
$unit = preg_replace("/[\\\*\.\w#: ]+\{([^\{\}]+)\}/", "\https://www.php1.cn/detail/\1", $arr_css[$i]);
$unit_list = preg_split("/;\s*/", $unit);
$unit = "\n";
for($j=0; $j if(strlen($unit_list[$j])>0)
$unit .= "    " . preg_replace("/\s*([\w\-]+)\s*:\s*(.*)/e", "'\https://www.php1.cn/detail/\1 : '.htmlspecialchars('\https://www.php1.cn/detail/\2').' ; '", $unit_list[$j]) . ($nl?"
\n":"\n");
}
$arr_css[$i] = $head . $unit . "}";
}
return join(($nl?"\n

\n":"\n\n"), $arr_css);
}


function script_highlight_note($str, $mode = true) {
$color_note = "gray";
$str = str_replace("\\\"","\"",$str);
$str = preg_replace("/(.*)<\/font>/isU", "\https://www.php1.cn/detail/\1", $str);
$str = $mode?"/*{$str}*/":"//{$str}";
return "{$str}";
}


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("|","&vertical;",$str);
$keywords = array(
"{$color_1}" => array("null", "true", "false", "NaN"),
"{$color_2}" => array("var", "for", "if", "else", "switch", "case", "function", "loop", "continue", "break", "<", ">", "!&equal;", "&equal;&equal;", "&equal;", "&&", "&vertical;&vertical;"),
"{$color_3}" => array("window", "self", "this", "event", "document", "opener", "Navigator", "Math", "Array", "String", "Object", "Function", "Number"),
);
foreach($keywords as $key => $value) {
for($i=0; $i $str = preg_replace("/(\W)(".$value[$i].")(\W)/", "\https://www.php1.cn/detail/\1\https://www.php1.cn/detail/\2\\3", $str);
}
}
$str = preg_replace("/(\.|\s)([a-z]+)\(/i" ,"\https://www.php1.cn/detail/\1\https://www.php1.cn/detail/\2(", $str);
$str = preg_replace("/\.(\w+)(?!\()/i" ,".\https://www.php1.cn/detail/\1", $str);
$str = preg_replace("/\/\/([^\n]*)\n/e" ,"script_highlight_note('\https://www.php1.cn/detail/\1', false)", $str);
$str = preg_replace("/\/\*(.*?)\*\//es" ,"script_highlight_note('\https://www.php1.cn/detail/\1')", $str);
$str = str_replace("\t", "    ", $str);
$str = str_replace("&equal;","=",$str);
$str = str_replace("&vertical;","|",$str);
return $nl?nl2br($str):$str;
}


function html_highlight_attr($att) {
$color_1 = "red";
$color_2 = "brown";
$color_3 = "black";
$att = str_replace("\\\"","\"",$att);
$att = preg_replace("/=\s*('|\")([^'\"]+)\https://www.php1.cn/detail/\1/eU","'=\https://www.php1.cn/detail/\1'.preg_replace('/\s+/','|:s:|','\https://www.php1.cn/detail/\2').'\https://www.php1.cn/detail/\1'",$att);
$att = str_replace("\\\"","\"",$att);
$att .= " ";
preg_match_all("/[a-z:\/\-]+(=(['\"]?).*[^\\\\]{1}\https://www.php1.cn/detail/\2?)?\s+/iU", $att, $att_list);
$att_list = $att_list[0];
for($i=0; $i if(empty($att_list[$i])) continue;
$att_list[$i] = trim($att_list[$i]);
$att_list[$i] = preg_replace("/([^=]+)([=]?)(['\"]?)(.*)(\\3)/", "\https://www.php1.cn/detail/\1\https://www.php1.cn/detail/\2\\3\\4\\3", $att_list[$i]);
}
return str_replace("|:s:|", " ", join(" ", $att_list));
}


function html_highlight($str) {
$color_1 = "red";
$color_2 = "blue";
$color_3 = "gray";
$str = str_replace("\\\"","\"",$str);
preg_match_all("/]*)>(.*)<\/script>/isU", $str, $arr_tmp);
$arr_script = $arr_tmp[0];
for($i=0; $i $str = str_replace($arr_script[$i], "
::script_{$i}::
", $str);
$arr_script[$i] = preg_replace("/]*)>(.*)<\/script>/iseU", "'<SCRIPT '.html_highlight_attr('\https://www.php1.cn/detail/\1').'>
'.script_highlight(str_replace('\\\"','\"','\https://www.php1.cn/detail/\2'), false).'</SCRIPT>'", $arr_script[$i]);
}
$str = preg_replace("/(]*>)(.*)(<\/textarea>)/ieU" ,"'\https://www.php1.cn/detail/\1'.htmlspecialchars('\https://www.php1.cn/detail/\2').'\\3'", $str);
$str = preg_replace("//seU" ,"''", $str);
$str = preg_replace("/]*)>(.*)<\/style>/isU" ,"\https://www.php1.cn/detail/\2", $str);
$str = preg_replace("/<([a-z:]+)\s*(.*?)( \/|\/)?>/ise" ,"'<\https://www.php1.cn/detail/\1 '.html_highlight_attr('\https://www.php1.cn/detail/\2').'\\3>'", $str);
$str = preg_replace("/<(\/[a-z:]+)>/i" ,"<\https://www.php1.cn/detail/\1>", $str);
$str = preg_replace("/<(!\-\-.*\-\-)>/sU" ,"<\https://www.php1.cn/detail/\1>", $str);
$str = preg_replace("/]*)>(.*)<\/style!>/iseU" ,"'<STYLE '.html_highlight_attr('\https://www.php1.cn/detail/\1').'>
'.css_highlight(str_replace('\\\"','\"','\https://www.php1.cn/detail/\2'), false).'
</STYLE>'", $str);
for($i=0; $i $str = str_replace("
::script_{$i}::
", $arr_script[$i], $str);
}
$str = str_replace("", "
", $str);
$str = str_replace("", "", $str);
$str = str_replace(" >", ">", $str);
$str = str_replace(" ","  ",$str);
$str = str_replace("\t","    ",$str);
return nl2br($str);
}


function get_file_pic($filename) {
global $root_path;
$ext = str_replace(".","",strrchr($filename,"."));
$pic = $root_path."images/mime_type/{$ext}.gif";
return file_exists($pic)?"$pic":$root_path."images/mime_type/attch.gif";
}


function get_dl_html($filename, $str = "") {
return " ".(empty($str)?basename($filename):$str)."";
}


function get_attach_html($filename, $id) {
return " $filename";
}


function get_vote_html($vote_idx, $message = ""){
global $db_host, $db_user, $db_pass, $db_name;
$db = new DB_Manager($db_host, $db_user, $db_pass);
$db->Connect();
$db->SelectDB($db_name);
$db->Query("select * from mnms_poll where id=".$vote_idx);
$record = $db->GetRS();
$str = "";
$db->close();
unset($db);
if($record) {
$username = "dummy";
$if_vote = !(strpos($record[vote_user], "|{$username}|")===false)
strpos($record[vote_user], "|".GetIp()."|");
$str .= "

".(empty($message)?$record[vote_title]:$message)."

";
$vote_list = split("::",$record[vote_list]);
$vote_count = split("::",$record[vote_count]);
$vote_users = count(split("\n", $record[vote_user])) - 1;
$vote_sum = array_sum($vote_count);
$str .= "
";
if($vote_sum==0) $vote_sum = 1;
for($i=0; $i $str .= "
\n";
}
$str .= "";
$str .= "
目前共有 {$vote_users} 人参与了本投票
".($if_vote?"► ":"").htmlspecialchars($vote_list[$i])." 占总票数的 ".ceil((int)$vote_count[$i]*100/$vote_sum)."%

".($if_vote?"您已经投过票了!":"   ")."


";
}
return $str;
}


function get_code_html($str) {
$str = str_replace("\\\"","\"",$str);
return "







(提示:可以先修改部分代码)
";
}


function get_list_html($content, $type = "") {
$content = preg_replace("/(
)?[\r\n]+/", "\n", $content);
$content = preg_replace("/[\r\n]+/", "\n", $content);
$content = preg_replace("/^[\n]*(.*)[\n]*$/m", "\https://www.php1.cn/detail/\1", $content);
$content = str_replace("\n", "
  • ", $content);
    $content = "
    • $content
    \n";
    return $content;
    }


    function html_trans($str) {
    $search = array("&", "'", "\"", "<", ">", " ", "\t");
    $replace = array("&", "'", """, "<", ">", "  ", "    ");
    return nl2br(str_replace($search, $replace, $str));
    }


    function link_url($str) {
    $str = preg_replace("/((http|ftp|mms|rtsp|pnm|mailto):\/\/[\w@:\.\/\?=&;#\-%]+)/i", "\https://www.php1.cn/detail/\1", $str);
    $str = preg_replace("/(\w+@(\w+\.)+[\w]{2,3})/i", "\https://www.php1.cn/detail/\1", $str);
    $str = preg_replace("/(href|src)='[^<]+<\/a>'/iU", "\https://www.php1.cn/detail/\1=\https://www.php1.cn/detail/\2", $str);
    return $str;
    }


    function htc2html_loop($str) {
    global $htc_list, $htc_list_att;
    foreach($htc_list as $key => $value) {
    if(substr($value,0,5)=="exec:"){
    $str = preg_replace("/\[$key\]((.(?!\[$key(\s?=\s?([^\]]+))?\]))*)\[\/$key\]/iexsU", substr($value,5), $str);
    } else {
    $str = preg_replace("/\[$key\]((.(?!\[$key(\s?=\s?([^\]]+))?\]))*?)\[\/$key\]/ixsU", "$value", $str);
    }
    }
    foreach($htc_list_att as $key => $value) {
    if(substr($value,0,5)=="exec:"){
    $str = preg_replace("/\[$key\s?=\s?([^\]]+)\]((.(?!\[$key(\s?=\s?([^\]]+))?\]))*)\[\/$key\]/iesxU", substr($value,5), $str);
    } else {
    $str = preg_replace("/\[$key\s?=\s?([^\]]+)\]((.(?!\[$key(\s?=\s?([^\]]+))?\]))*)\[\/$key\]/isxU", "$value", $str);
    }
    }
    return $str;
    }


    function htc2html($str, $trans_url = true , $loop = 3) {
    global $htc_list_spl;
    $str = preg_replace("/(\[\w+\])\s*/ixs","\https://www.php1.cn/detail/\1",$str);
    $str = preg_replace("/\s*(\[\/\w+\])/ixs","\https://www.php1.cn/detail/\1",$str);
    foreach($htc_list_spl as $key => $value) {
    preg_match_all("/\[$key\](.*)\[\/$key\]/ixsU", $str, $arr_tmp);
    $arr_spl[$key] = $arr_tmp[0];
    for($i=0; $i $str = str_replace($arr_spl[$key][$i], "
    ::{$key}_{$i}::
    ", $str);
    if(substr($value,0,5)=="exec:"){
    $arr_spl[$key][$i] = preg_replace("/\[$key\](.*)\[\/$key\]/iexsU", substr($value,5), $arr_spl[$key][$i]);
    } else {
    $arr_spl[$key][$i] = preg_replace("/\[$key\](.*)\[\/$key\]/ixsU", "$value", $arr_spl[$key][$i]);
    }
    }
    }
    $str = html_trans($str);
    while($loop-- > 0) {
    if(!preg_match("/\[(\w+)(\s?=\s?([^\]]+))?\](.*)\[\/\https://www.php1.cn/detail/\1\]/isU", $str)) break;
    $str = htc2html_loop($str);
    }
    if($trans_url) $str = link_url($str);
    foreach($htc_list_spl as $key => $value) {
    for($i=0; $i $str = str_replace("
    ::{$key}_{$i}::
    ", $arr_spl[$key][$i], $str);
    }
    }
    return $str;
    }
    ?>



    使用方法:

    echo htc2html($str);

    OK 了~~~~~~~~

    www.phpzy.comtrue/php/6641.htmlTechArticlePHP-HtmlTransferCode PHP - Html Transfer Code超文本转换码,市面上这东西已经很滥(多)了,不过是这几天自己实践了一下~~~~ 支持 多重嵌套 和 同标签嵌套,混合高亮显示(还有一点小问题)...

  • 相关文章

    相关频道:

    PHP之友评论

    今天推荐