PHP - Html Transfer Code_PHP教程

php中文网
发布: 2016-07-21 16:09:54
原创
1162人浏览过


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"                => "\1",
                "I"                => "\1",
                "U"                => "\1",
                "CENTER"        => "

\1
",
                "URL"                => "\1",
                "EMAIL"                => "\1",
                "IMAGE"                => "
@@##@@parseInt(this.offsetParent.offsetWidth))?this.offsetParent.offsetWidth:org_w' onclick='window.open(this.src)' style='cursor:pointer' />

",
                "DOWNLOAD"        => "exec:get_dl_html('\1')",
                "LIST"                => "exec:get_list_html('\1')",
                "QUOTE"                => "
  引用:
\1
",
                "IFRAME"        => "
",
                "MOVE"                => "
\1
",
                "GLOW"                => "
\1
",
                "SHADOW"        => "
\1
",
                "DROPSHADOW"        => "
\1
",
                "GBMUSIC"        => " ",
                "MUSIC"                => "
@@@###@@@
Download The Music
",               
                "FLASH"                => "
@@@###@@@
",
                "SHOCKWAVE"        => "
@@@###@@@
",
                "QUICKTIME"        => "
@@@###@@@
Download The Movie
",
                "REALPLAYER"        => "
@@@###@@@
Download The Movie
",
                "MEDIAPLAYER"        => "
@@@###@@@
Download The Movie
",
                "VOTE"                => "exec:get_vote_html('\1')",
                );

$htc_list_att = array(
                "ALIGN"                => "
\2
",
                "URL"                => "\2",
                "EMAIL"                => "\2",
                "IMAGE"                => "
@@##@@parseInt(this.offsetParent.offsetWidth))?this.offsetParent.offsetWidth:org_w;' onclick='window.open(this.src)' style='cursor:pointer' />

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

":" "), $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 "{$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)/", "\1\2\3", $str);
                }
        }
        $str = preg_replace("/(.|s)([a-z]+)(/i" ,"\1\2(", $str);
        $str = preg_replace("/.(w+)(?!()/i" ,".\1", $str);
        $str = preg_replace("///([^ ]*) /e" ,"script_highlight_note('\1', false)", $str);
        $str = preg_replace("//*(.*?)*//es" ,"script_highlight_note('\1')", $str);       
        $str = str_replace(" ", "    ", $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*('|")([^'"]+)\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                if(empty($att_list[$i])) continue;
                $att_list[$i] = trim($att_list[$i]);
                $att_list[$i] = preg_replace("/([^=]+)([=]?)(['"]?)(.*)(\3)/", "\1 font>\2 font>\3\4 font>\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>]*)>(.*)</script>/isU", $str, $arr_tmp);<br> $arr_script = $arr_tmp[0];<br> for($i=0; $i<count($arr_script); $i++) {<br> $str = str_replace($arr_script[$i], "<br>::script_{$i}::<br>", $str);<br> $arr_script[$i] = preg_replace("/<script([^>]*)>(.*)</script>/iseU", "'<font color='{$color_2}'><SCRIPT '.html_highlight_attr('\1').'></script>

'.script_highlight(str_replace('\"','"','\2'), false).''", $arr_script[$i]);
        }
        $str = preg_replace("/(

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


function get_list_html($content, $type = "") {
        $content = preg_replace("/(
)?[ ]+/", " ", $content);
        $content = preg_replace("/[ ]+/", " ", $content);
        $content = preg_replace("/^[ ]*(.*)[ ]*$/m", "\1", $content);
        $content = str_replace(" ", "
  • ", $content);
            $content = "
    • $content
    ";
            return $content;
    }


    function html_trans($str) {
            $search  = array("&",                "'",                """,                "",        "  ",                " ");
            $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("/[$keys?=s?([^]]+)]((.(?![$key(s?=s?([^]]+))?]))*)[/$key]/iesxU", substr($value,5), $str);
                    } else {
                            $str = preg_replace("/[$keys?=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                        $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?([^]]+))?](.*)[/\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.bkjia.comtruehttp://www.bkjia.com/PHPjc/314366.htmlTechArticlePHP - Html Transfer Code超文本转换码,市面上这东西已经很滥(多)了,不过是这几天自己实践了一下~~~~ 支持 多重嵌套 和 同标签嵌套,混合...
    PHP - Html Transfer Code_PHP教程PHP - Html Transfer Code_PHP教程PHP - Html Transfer Code_PHP教程
  • 相关标签:
    PHP速学教程(入门到精通)
    PHP速学教程(入门到精通)

    PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

    下载
    来源:php中文网
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    开源免费商场系统广告
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号