文章标题、内容、摘要的处理函数

Imports System.Data
Imports System.Text.RegularExpressions
Imports System.Data.OleDb
Public Class ShowArticle
    Inherits System.Web.UI.Page

#Region " Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents LblTitle As System.Web.UI.WebControls.Label
    Protected WithEvents LblContent As System.Web.UI.WebControls.Label

    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码
        Dim StrArticleId As String
        StrArticleId = Request("ArticleId")
        Dim myArticle As New AccessDb
        Dim myReader As OleDbDataReader
        Dim myStrTitle As String
        myReader = myArticle.ExecuteSqlStrDetails("select * from portal_Articles where articleid=" + StrArticleId + ";")
        If myReader.Read Then
            myStrTitle = myReader("title")
            If strlen(myStrTitle) > 20 Then
                myStrTitle = gotTopic(myStrTitle, 20)
            End If
            LblTitle.Text = myStrTitle
            LblContent.Text = HTMLEncode(myReader("Content"))
        End If
    End Sub
    Function HTMLEncode(ByVal reString As String) '转换HTML代码
        Dim Str As String = reString
        If Str.Length > 0 Then
            Str = Replace(Str, ">", "&gt;")
            Str = Replace(Str, "<", "&lt;")
            Str = Replace(Str, Chr(9), "&nbsp;")
            Str = Replace(Str, Chr(39), "'")
            Str = Replace(Str, Chr(34), "&quot;")
            Str = Replace(Str, Chr(13), "")
            Str = Replace(Str, Chr(10), "<br/>")
            HTMLEncode = Str
        End If
    End Function

    '得到字符串长度
    Function strlen(ByVal str As String) As Integer
        Dim p_len As Integer
        Dim xx As Integer
        p_len = 0
        strlen = 0
        If Trim(str) <> "" Then '去掉空格
            p_len = Len(Trim(str))
            For xx = 1 To p_len
                If Asc(Mid(str, xx, 1)) < 0 Then
                    strlen = Int(strlen) + 2
                Else
                    strlen = Int(strlen) + 1
                End If
            Next
        End If
    End Function
    '得到标题,如果多于strlen宽度,则以...显示
    Function gotTopic(ByVal str As String, ByVal strlen1 As String) As String
        Dim l, t, c, i As Integer
        l = Len(str)
        t = 0
        For i = 1 To l
            c = Math.Abs(Asc(Mid(str, i, 1)))
            If c > 255 Then
                t = t + 2
            Else
                t = t + 1
            End If
            If t >= strlen1 Then
                gotTopic = Left(str, i) & "..."
                Exit For
            Else
                gotTopic = str & " "
            End If
        Next
    End Function


    Function RemoveHtml(ByVal str) As String '过滤Html格式
        Dim re As Regex
        str = re.Match(str, "(\<.[^\<]*\>)")
        str = re.Replace(str, " ")
        str = re.Match(str, "(\<\/[^\<]*\>")
        str = re.Replace(str, "")
        RemoveHtml = str
        re = Nothing
    End Function
End Class

原文地址:https://www.cnblogs.com/zhangchenliang/p/577810.html