0

データベースにいくつかの記事が保存されています。特定のページでは、いくつかの設定に基づいて記事の特定の割合を表示したいと考えていました。例: 記事の 80%

問題は、文字列の長さの特定の割合を使用するとhtmlがプレーンテキストではないため、フォーマットが乱れることです。私が試した書式設定を乱すことなく、切り捨てられた html を返します

Private Function HtmlSubstring(html As String, maxlength As Integer) As String
        'initialize regular expressions
        Dim htmltag As String = "</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>"
        Dim emptytags As String = "<(\w+)((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?></\1>"

        'match all html start and end tags, otherwise get each character one by one..
        Dim expression As Regex = New Regex(String.Format("({0})|(.?)", htmltag))
        Dim matches As MatchCollection = expression.Matches(html)

        Dim i As Integer = 0
        Dim content As New StringBuilder()
        For Each match As Match In matches
            If match.Value.Length = 1 AndAlso i < maxlength Then
                content.Append(match.Value)
                i += 1
                'the match contains a tag
            ElseIf match.Value.Length > 1 Then
                content.Append(match.Value)
            End If
        Next

        Return Regex.Replace(content.ToString(), emptytags, String.Empty)
    End Function

しかし、いつもうまくいかなかった

4

2 に答える 2

1

あなたが求めていることを行うための組み込みの.NETメソッドはないと確信しています。ただし、次の方法を検討してください。

HTML ページはおそらく構造化されています。つまり、段落や見出しなどがあります。

<h1>...</h1>
<p>...</p>
<h2>...</h2>
<p>...<more tags>...</more tags></p>
<h2>...</h2>
<p>...</p>
...

あなたができることは次のとおりです。

  1. HTML パーサー ( HTML アジリティ パックは、このコンテキストでよく言及されます) を使用して、HTML をデータ構造に解析します。
  2. トップレベルのタグの最初の 80%を取得します。たとえば、HTML コンテンツのルート ノードに 10 個の子がある場合、最初の 8 個を取得します。

    <h1>...</h1>
    <p>...</p>
    <p>...</p>
    <h2>...</h2>
    <p>
       ...
       <more tags>
          ...
       </more tags>
       ...
    </p>
    <p>...</p>
    <p>...<more tags>...</more tags>...</p>
    <p>...</p>
    ---------------
    <h2>...</h2>
    <p>...</p>
    

記事がほぼ均等に配置されている場合 (つまり、長い段落と短い段落が記事全体で平均化されている場合)、HTML の書式設定を崩さずにテキストの約80% を表示できます。追加の利点として、テキストを行または段落の途中で分割しません。

于 2013-03-01T07:02:24.450 に答える
0

最後に、以下は私にとって非常にうまく機能しています

 Private Function HtmlSubstring(ByRef html As String, maxlength As Integer) As String
    'initialize regular expressions
    Const htmltag As String = "</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>"
    'match all html start and end tags, otherwise get each character one by one..
    Dim expression As Regex = New Regex(String.Format("({0})|(.?)", htmltag))
    Dim matches As MatchCollection = expression.Matches(html)
    Dim i As Integer = 0
    Dim isEndingSet As Boolean = False
    Dim content As StringBuilder = New StringBuilder()
    For Each match As Match In matches
        If match.Value.Length = 1 AndAlso i < maxlength Then
            content.Append(match.Value)
            'the match contains a tag
            i += 1
        ElseIf match.Value.Length > 1 Then
            If (isEndingSet AndAlso (match.Value.ToLower() = "<br />" OrElse match.Value.ToLower() = "<br>")) Then
                Continue For
            End If
            content.Append(match.Value)
        End If
        If (i = maxlength AndAlso Not isEndingSet) Then
            content.Append("....")
            isEndingSet = True
        End If
    Next

    Return content.ToString()
End Function
于 2013-03-08T07:22:53.063 に答える