处理HTML代码的若干函数

经过了上个星期的努力学习,对处理html又有了新的发现
感觉真的很不错
可以说js的威力在处理html代码方面我又有所领悟了
1、截取特定长度字符串

    Public Shared Function CutString1(ByVal str As StringByVal length As IntegerAs String


        
Dim i As Integer = 0
        
Dim j As Integer = 0
        
For Each chr As Char In str
            
If Asc(chr> 127 Then
                i 
= i + 2
            
Else
                i 
+= 1
                
If (i > length) Then
                    
str = str.Substring(0, j) + ""
                    
Exit For
                
End If
            
End If
            j 
+= 1
        
Next
        
Return str

    
End Function
2、在在textarea(textbox里面的textmode为dul),对其输入的字符转换成html格式
Public Function HtmlEncode(ByVal str As StringAs String
        
Dim L As Integer
        
Dim result As String
        
If str = "" Then
            
Return ""
        
End If
        L 
= Len(str)
        
Dim i As Integer
        
For i = 1 To L
            
Select Case Mid(str, i, 1)
                
Case Chr(13)
                    result 
= result + "<br>"
                
Case Chr(34)
                    result 
= result + "&quot;"
                
Case Chr(32)
                    
'result=result+"&nbsp;"
                    If i + 1 <= L And i - 1 > 0 Then
                        
If Mid(str, i + 11= Chr(32Or Mid(str, i + 11= Chr(9Or Mid(str, i - 11= Chr(32Or Mid(str, i - 11= Chr(9Then
                            result 
= result + "&nbsp;"
                        
Else
                            result 
= result + " "
                        
End If
                    
Else
                        result 
= result + "&nbsp;"
                    
End If
                
Case Chr(9)
                    result 
= result + "    "
                
Case Else
                    result 
= result + Mid(str, i, 1)
            
End Select
        
Next
        
Return result
    
End Function

3、在textarea(textbox里面的textmode为dul),对其输入的字符转换成html格式输出
    Public Shared Function StripHTML(ByVal strHtml As StringAs String
        
Dim aryReg() As String = New String() {"<script[^>]*?>.*?</script>""<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>""([\r\n])[\s]+""&(quot|#34);""&(amp|#38);""&(lt|#60);""&(gt|#62);""&(nbsp|#160);""&(iexcl|#161);""&(cent|#162);""&(pound|#163);""&(copy|#169);""&#(\d+);""-->""<!--.*\n"}

        
Dim aryRep() As String = {"""""""""""&"""""" ""\xa1"Chr(161), "\xa2"Chr(162), "\xa3"Chr(163), "\xa9"Chr(169), """\r\n"""}





        
Dim NewReg As String = aryReg(0)
        
Dim strOutput As String = strHtml
        
Dim i As Integer
        
For i = 0 To aryReg.Length - 1 Step i + 1
            
Dim regex As Regex = New Regex(aryReg(i), RegexOptions.IgnoreCase)
            strOutput 
= regex.Replace(strOutput, aryRep(i))
        
Next

        strOutput.Replace(
"<""")
        strOutput.Replace(
">""")
        strOutput.Replace(
"\r\n""")


        
Return strOutput
    
End Function
4、一个plus js代码,作用是把所有的tooltip格式化后转变一个层,特定输出(效果见http://www.lostfound.cn)
var pltsPop=null;
var pltsoffsetX = 10;
var pltsoffsetY = 15;
var pltsPopbg="#ffff00";
var pltsPopfg="#111111";
var pltsTitle="";
document.write('
<div id=pltsTipLayer style="display: none;position: absolute; z-index:10001"></div>');
function pltsinits()
{
    document.onmouseover   
= plts;
    document.onmousemove 
= moveToMouseLoc;
}

function plts()
{  var o=event.srcElement;
    
if(o.alt!=null && o.alt!=""){o.dypop=o.alt;o.alt=""};
    
if(o.title!=null && o.title!=""){o.dypop=o.title;o.title=""};
    pltsPop
=o.dypop;
    
if(pltsPop!=null&&pltsPop!=""&&typeof(pltsPop)!="undefined")
    
{
pltsTipLayer.style.left
=-1000;
pltsTipLayer.style.display
='';
var Msg=pltsPop.replace(/\n/g,"<br>");
Msg
=Msg.replace(/\0x13/g,"<br>");
var re=/\{(.[^\{]*)\}/ig;
if(!re.test(Msg))pltsTitle="[MyTopWin.Com] 拓文CMS ASP.net系统";
else{
   re
=/\{(.[^\{]*)\}(.*)/ig;
     pltsTitle
=Msg.replace(re,"$1")+"&nbsp;";
   re
=/\{(.[^\{]*)\}/ig;
   Msg
=Msg.replace(re,"");
   Msg
=Msg.replace("<br>","");}

  
        
var content =
       '
<table style="FILTER:alpha(opacity=90) shadow(color=#bbbbbb,direction=150);" id=toolTipTalbe border=0><tr><td width="100%"><table class=selet_bg cellspacing="0" cellpadding="0" style="width:100%" style="font size:11px;">'+
       '
<tr id=pltsPoptop><th height=12 valign=bottom class=header><p id=topleft align=left>↖'+pltsTitle+'</p><p id=topright align=right style="display:none">'+pltsTitle+'↗</font></th></tr>'+
       '
<tr><td style="padding-left:10px;padding-right:10px;padding-top: 4px;padding-bottom:4px;line-height:135%">'+Msg+'</td></tr>'+
       '
<tr id=pltsPopbot style="display:none"><th height=12 valign=bottom class=header><p id=botleft align=left>↙'+pltsTitle+'</p><p id=botright align=right style="display:none">'+pltsTitle+'↘</font></th></tr>'+
       '
</table></td></tr></table>';
        pltsTipLayer.innerHTML
=content;
        toolTipTalbe.style.width
=Math.min(pltsTipLayer.clientWidth,document.body.clientWidth/2.2);
        moveToMouseLoc();
        
return true;
       }

    
else
    
{
     pltsTipLayer.innerHTML
='';
       pltsTipLayer.style.display
='none';
        
return true;
    }

}


function moveToMouseLoc()
{
if(pltsTipLayer.innerHTML=='')return true;
var MouseX=event.x;
var MouseY=event.y;
//window.status=event.y;
var popHeight=pltsTipLayer.clientHeight;
var popWidth=pltsTipLayer.clientWidth;
if(MouseY+pltsoffsetY+popHeight>document.body.clientHeight)
{
    popTopAdjust
=-popHeight-pltsoffsetY*1.5;
    pltsPoptop.style.display
="none";
    pltsPopbot.style.display
="";
}

  
else
{
     popTopAdjust
=0;
    pltsPoptop.style.display
="";
    pltsPopbot.style.display
="none";
}

if(MouseX+pltsoffsetX+popWidth>document.body.clientWidth)
{
  popLeftAdjust
=-popWidth-pltsoffsetX*2;
  topleft.style.display
="none";
  botleft.style.display
="none";
  topright.style.display
="";
  botright.style.display
="";
}

else
{
  popLeftAdjust
=0;
  topleft.style.display
="";
  botleft.style.display
="";
  topright.style.display
="none";
  botright.style.display
="none";
}

pltsTipLayer.style.left
=MouseX+pltsoffsetX+document.body.scrollLeft+popLeftAdjust;
pltsTipLayer.style.top
=MouseY+pltsoffsetY+document.body.scrollTop+popTopAdjust;
   
return true;
}

pltsinits();
posted @ 2006-05-28 19:58  徐灿钊Asp.net专栏  阅读(1788)  评论(1编辑  收藏  举报