加龙

加龙加香不加价
随笔 - 133, 评论 - 343 , 引用 - 51

不使用正则表达式去掉HTML代码函数

常碰到过滤掉HTML标记的情况,在网上有很多网友写的用正则表达式方法,很好用!
但在IE5.0上面,正则表达式不起作用,于是我就写了这个小函数来把网页转换为TXT文本

VBs代码

Function filterstr(dtitle)                  '去HTML代码的函数
    On Error Resume Next
    Dim topic, i As Integer
    topic = Split(dtitle, ">")
    dtitle = ""
    For i = 0 To UBound(topic)
        If InStrRev(topic(i), "<") > 0 Then
            dtitle = dtitle & Left(topic(i), InStrRev(topic(i), "<"))
        Else
            dtitle = dtitle & topic(i)
        End If
DoEvents
    Next
    filterstr = Replace(dtitle, "<", "")     '去掉HTML标记
End Function

发表于 2005年4月15日 19:29

评论

# re: 不使用正则表达式去掉HTML代码函数

这个是不严密的,因为网页里面允许>号作为显示内容存在的:)
2005-4-26 22:39 | 铁匠

# re: 不使用正则表达式去掉HTML代码函数

/// <summary>
/// 替换掉所有的html标记
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string wipeHtml(string html)
{
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<[\/\!]*?[^<>]*?>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html = regex1.Replace(html, "");
return html;
}
2007-3-13 12:13 | ddf3

Post Comment

主题  
姓名  
主页
校验码  
内容   
京ICP备 05050892号