26. maj 2006 - 14:27Der er
6 kommentarer og 1 løsning
Fjern alle HTML tags fra en tekst
hejsa Jeg har en db kolonne med en tekst indeholdende html tags. Jeg vil på min oversigtside gerne udskrive de første fx. 200 tegn, men inden jeg kan gøre det skal jeg have fjernet alle html tags - så jeg ikke bryder midt i et tag.
En tekst kunne fx være:
<P><STRONG>Dette er et eksempel</STRONG> på hvordan en tekst i <U>databasen</U> kan være formateret med <FONT color=#cc0033><STRONG>HTML</STRONG></FONT> tags. Punktopstilling:</P><UL><LI>punkt1</LI><LI>punkt2</LI></UL><P align=center><EM>Disse skal fjernes så teksten bliver ren-tekst.</EM></P>
soudo kode: ****************** tekstHTML = tekstindeholdende_HTMLTAGS tekst = Replace_ALL_HTML_TAGS_IN(tekstHTML)
If Len(tekst) < 201 Then Response.Write tekst Else Response.Write left(tekst,149) Response.Write Split(Mid(tekst,150,200), ".")(0) & "." End If *******************
Skal nok være noget med et regulært udtryk, men det er bare overhoved ikke noget jeg kender til at skrive - nogle der lige kan klare den?
I min søgen fandt jeg denne. Jeg mindes at eagleeyes er bedre. (Denne har jeg ikk selv testet, da jeg ikk har mulighed for den lige pt)
<% Function RemoveHTML(strContent) Do pos = 1 tmpBegin = InStr(pos,strContent,"<") If tmpBegin > 0 Then pos = tmpBegin If LCase(mid(strContent,tmpBegin,7)) = "<script" Then ' Her fjernes script kode tmpEnd = Instr(tmpBegin,LCase(strContent),"</script>") strContent = Left(strContent,tmpBegin-1) & Right(strContent,Len(strContent)-tmpEnd-8) ElseIf LCase(mid(strContent,tmpBegin,2)) = Chr(60) & "%" Then ' Her fjernes ASP kode tmpEnd = Instr(tmpBegin,LCase(strContent),"%" & Chr(62)) strContent = Left(strContent,tmpBegin-1) & Right(strContent,Len(strContent)-tmpEnd-3) Else ' Her fjernes HTML kode tmpEnd = InStr(pos,strContent,">") If tmpEnd > 0 Then strContent = Left(strContent,tmpBegin-1) & Right(strContent,Len(strContent)-tmpEnd) Else strContent = Left(strContent,tmpBegin-1) Exit Do End If End If Else Exit Do End If Loop RemoveHTML = strContent End Function
tmpContent = Trim(Request.Form("HTML")) If Len(tmpContent) > 0 Then Response.Write Replace(RemoveHTML(tmpContent), vbCrLf, "<br>") Response.End End If %>
' Purpose: This function clears all HTML tags from a ' string using Regular Expressions. ' Inputs: strHTML; ' A string to be cleared of HTML TAGS ' intWorkFlow; ' An integer that if equals to 0 runs only the RegExp filter ' .. 1 runs only the HTML source render filter ' .. 2 runs both the RegExp and the HTML source render ' .. >2 defaults to 0 ' Returns: A string that has been filtered by the function
function ClearHTMLTags(strHTML, intWorkFlow) 'Variables used in the function
dim regEx, strTagLess
'--------------------------------------- strTagless = strHTML 'Move the string into a private variable 'within the function '---------------------------------------
'regEx initialization '--------------------------------------- set regEx = New RegExp 'Creates a regexp object regEx.IgnoreCase = True 'Don't give frat about case sensitivity regEx.Global = True 'Global applicability '---------------------------------------
'Phase I ' "bye bye html tags" if intWorkFlow <> 1 then '--------------------------------------- regEx.Pattern = "<[^>]*>" 'this pattern mathces any html tag strTagLess = regEx.Replace(strTagLess, "") 'all html tags are stripped '--------------------------------------- end if
'Phase II ' "bye bye rouge leftovers" ' "or, I want to render the source" ' "as html."
'--------------------------------------- 'We *might* still have rouge < and > 'let's be positive that those that remain 'are changed into html characters '---------------------------------------
if intWorkFlow > 0 and intWorkFlow < 3 then regEx.Pattern = "[<]" 'matches a single < strTagLess = regEx.Replace(strTagLess, "<")
regEx.Pattern = "[>]" 'matches a single > strTagLess = regEx.Replace(strTagLess, ">") '--------------------------------------- end if
'Clean up '--------------------------------------- set regEx = nothing 'Destroys the regExp object '---------------------------------------
'--------------------------------------- ClearHTMLTags = strTagLess 'The results are passed back '--------------------------------------- end function %>
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.