找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 451|回复: 0
打印 上一主题 下一主题

PHP - Html Transfer Code

[复制链接]

2487

主题

2487

帖子

7391

积分

论坛元老

Rank: 8Rank: 8

积分
7391
跳转到指定楼层
楼主
发表于 2018-2-14 09:46:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

                                                           
                        
PHP - Html Transfer Code
超文本转换码,市面上这东西已经很滥(多)了,不过是这几天自己实践了一下~~~~
支持 多重嵌套 和 同标签嵌套,混合高亮显示(还有一点小问题)
自我感觉便于修改和扩充~~~~~~~~~~~
"\\1",
                "I"                => "\\1",
                "U"                => "\\1",
                "CENTER"        => "\\1\n",
                "URL"                => "\\1",
                "EMAIL"                => "\\1",
                "IMAGE"                => "


parseInt(this.offsetParent.offsetWidth))?this.offsetParent.offsetWidthrg_w' onclick='window.open(this.src)' style='cursor:pointer' />

\n",
                "DOWNLOAD"        => "exec:get_dl_html('\\1')",
                "LIST"                => "exec:get_list_html('\\1')",
                "QUOTE"                => "
  引用:
\\1
\n",
                "IFRAME"        => "
\n",
                "MOVE"                => "\\1\n",
                "GLOW"                => "
\\1
\n",
                "SHADOW"        => "
\\1
\n",
                "DROPSHADOW"        => "
\\1
\n",
                "GBMUSIC"        => "\n",
                "MUSIC"                => "
[TR][TD]
Download The Music[/TD][/TR]
\n",               
                "FLASH"                => "
\n",
                "SHOCKWAVE"        => "
\n",
                "QUICKTIME"        => "

Download The Movie
\n",
                "REALPLAYER"        => "
[TR][TD]
Download The Movie[/TD][/TR]
\n",
                "MEDIAPLAYER"        => "

Download The Movie
\n",
                "VOTE"                => "exec:get_vote_html('\\1')",
                );
$htc_list_att = array(
                "ALIGN"                => "\\2\n",
                "URL"                => "\\2",
                "EMAIL"                => "\\2",
                "IMAGE"                => "


parseInt(this.offsetParent.offsetWidth))?this.offsetParent.offsetWidthrg_w;' onclick='window.open(this.src)' style='cursor:pointer' />

\n",
                "FONTFACE"        => "\\2",
                "FONTSIZE"        => "\\2",
                "FONTCOLOR"        => "\\2",
                "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(
                "HP"                => "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 = "";
        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; $i0)
                                $unit .= "    " . preg_replace("/\s*([\w\-]+)\s*:\s*(.*)/e", "'
[color=]\\1
:
[color=]'.htmlspecialchars('\\2').'
; '", $unit_list[$j]) . ($nl?"
\n":"\n");
                }
                $arr_css[$i] = $head . $unit . "
[color=]}
";
        }
        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("/(.*)/isU", "\\1", $str);
        $str = $mode?"/*{$str}*/":"//{$str}";
        return "
[color=]{$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\\1\\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("/'", $arr_script[$i]);
        }
        $str = preg_replace("/(]*>)(.*)()/ieU" ,"'\\1'.htmlspecialchars('\\2').'\\3'", $str);
        $str = preg_replace("//seU" ,"''", $str);
        $str = preg_replace("/]*)>(.*)/isU" ,"\\2", $str);
        $str = preg_replace("//ise" ,"''", $str);
        $str = preg_replace("//i" ,"", $str);
        $str = preg_replace("//sU" ,"", $str);
        $str = preg_replace("/]*)>(.*)/iseU" ,"'
'.css_highlight(str_replace('\\\"','\"','\\2'), false).'
'", $str);
        for($i=0; $i", "", $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 .= "
                             目前共有
{$vote_users}
人参与了本投票 ";
                if($vote_sum==0) $vote_sum = 1;
                for($i=0; $i
                        ";
        }
        return $str;
}
function get_code_html($str) {
        $str = str_replace("\\\"","\"",$str);
        return "
        [table]
          ".htmlspecialchars($str)."
            
            
            
            (提示:可以先修改部分代码)
          ";
}
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", "\\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", "\\1", $str);
            $str = preg_replace("/(\w+@(\w+\.)+[\w]{2,3})/i", "\\1", $str);
            $str = preg_replace("/(href|src)='[^'/iU", "\\1=\\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","\\1",$str);
            $str = preg_replace("/\s*(\[\/\w+\])/ixs","\\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 0) {
                    if(!preg_match("/\[(\w+)(\s?=\s?([^\]]+))?\](.*)\[\/\\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
    使用方法:
    echo htc2html($str);
    OK 了~~~~~~~~
                        
                         
                
                
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端