ANSI颜色码转HTML

80酷酷网    80kuku.com

  此方法是一字一字往下抓ANSI颜色码转换,有点土法炼钢。当前和大部分TELNET软件
比较不同是他们换行就有自动作退出码,而我这个并没有作。

 
<?
Echo 
"<body bgcolor=000000><PRE  face=新宋体>;
Echo 
ANSI_2_HTML(Fread(Fopen("1.txt","r"),FileSize("1.txt"
)));
Echo 
"</pre>"
;
## ANSI 转换成 HTML
Function ANSI_2_HTML($String
){

   
$ANSI_Light=Array("0"=>"000000","1"=>"#FF0000","2"=>"#00CC00","3"=>"#FFFF00","4"=>"#3366FF","5"=>"#CC33CC","6"=>"#66FFFF","7"=>"#FFFFFF"
);
   
$ANSI_Dark =Array("0"=>"000000","1"=>"#840000","2"=>"#008200","3"=>"#AD4100","4"=>"#000084","5"=>"#840084","6"=>"#00B6B5","7"=>"#C6C7C6"
);
   
   
## 颜色样本同CNEWS      

   
$flag_have_ansi0
;
   
$dl            0
;         
   
$flag_hava_bg  0
;

   
$String=Str_replace(Chr(27).'[m',Chr(27).'[0m',$String
);
   
   For(
$i=0;$i<Strlen($String);$i
++):
      If(
$String[$i]==Chr(27
)):
         
$lastString=Substr($String,$i,Strlen($String)-$i
);         
         If(
Ereg(Chr(27)."\[([0-8;]{0,}[^m])m",$lastString
)):
            
$ansicode=Substr($lastString,2,Strpos($lastString,'m')-2
);
            
$i=$i+Strpos($lastString,'m'
);
            
$Rendition=Split(";",$ansicode
);
            If(
Is_Array($Rendition)) Sort($Rendition
);
            For(
$j=0;$j<=count($Rendition)-1;$j
++):
               
$Rendition[$j]=(int)$Rendition[$j
];
               If(
$Rendition[$j]=='1'$dl=1
;
               If(
$Rendition[$j]=='0'$dl=0
;
               
               
$ansi=($dl=='0') ? $ANSI_Dark $ANSI_Light
;

               If(
$Rendition[$j]<10
):
                  If(
$Rendition[$j]==0
) :
                     
$style="background-color:#000000;text-decoration:none;"
;
                     
$tempBg=""
;
                  EndIf;
                  If(
$Rendition[$j]==1

                     
$style="color:#ffffff;"
;
                  If(
$Rendition[$j]==4

                     
$style=$style."text-decoration:underline;"
;            
                  
# 5 blink on
                  # 7 reverse video on
                  # 8 nondisplayed (invisible) 缺 不知道如何实作显示,但大部分后三者用不到
               
EndIf;               
               If(
$Rendition[$j]>=30&&$Rendition[$j]<=37
):
                  
$style=$style."color:".$ansi[$Rendition[$j]%10].";"
;
                  
$style=$style.$tempBg
;
               EndIf;
               If(
$Rendition[$j]>=40
):               
                  
$flag_hava_bg 1
;
                  
$style=$style."background-color:".$ANSI_Dark[$Rendition[$j]%10].";"
;                  
                  
$tempBg="background-color:".$ANSI_Dark[$Rendition[$j]%10].";"
;
               EndIf;
            EndFor;
            
            If(
$flag_have_ansi==1
):
               
$font="</span><span  face=新宋体>;
            ElseIf(
$flag_have_ansi==0
):
               
$flag_hava_bg 0
;
               
$tempBg       =""
;
               
$font="<span  face=新宋体>;
            EndIf;
            
$flag_have_ansi=1
;
            
$myansi.=$font
;   
         EndIf;
      Else:
         
$myansi.=$String[$i
];
      EndIf;
   EndFor;
   Return 
"</span>".$myansi
;   
}


下图为TELNET软件浏览


下图为浏览器浏览


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间

上一篇JPG,PNG,GIF转Ascii

下一篇WORD排版

点击: