我有一些文章保存在数据库中。在某些页面上,我想根据某些设置显示一定百分比的文章。例如文章的 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
但并不总是有效