Ooops
January 26, 2021, 04:55:33 AM

Author Topic: [bmx] deHTML by xlsior [ 1+ years ago ]  (Read 520 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] deHTML by xlsior [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : deHTML
Author : xlsior
Posted : 1+ years ago

Description : Function that strips all HTML tags from a string, returning the remaining text without tags

Code :
Code: BlitzMax
  1. ' Basic DeHTML function
  2. ' Strips HTML tags And keeps the readable non-HTML portions
  3. '
  4. ' By Marc van den Dikkenberg / xlsior
  5. ' http://www.xlsior.org
  6. '
  7. ' For more extended HTML codes, see: http://www.ascii.cl/htmlcodes.htm
  8. ' Note that the extended HTML codes such as the pound symbol are currently ignored.
  9. '
  10.  
  11.  
  12. SuperStrict
  13.  
  14. Local Sample:String="<h1>This</h1><br><b><i> is</i> just</b> some <p>"sample"-html</p> <i>code</i><br><br><br><br>"
  15.  
  16. Print "Unmodified String: "
  17. Print Sample:String
  18. Print ""
  19. Print "Stripped String: "
  20. Print DeHTML(Sample:String)
  21.  
  22.  
  23. Function DeHTML:String(SomeString:String)
  24.         Local dehtmlmode:Int=False
  25.         Local detempstring:String=""
  26.         Local detempcounter:Int=0
  27.  
  28.         ' Check if it's possible that there are any &..; HTML codes -- if so, search & replace them
  29.         If Instr(SomeString,"&")<>0 And Instr(SomeString,";")<>0 Then
  30.                 SomeString=Replace(SomeString,"& nbsp ;"," ")
  31.                 SomeString=Replace(SomeString,"& quot ;",Chr$(34))
  32.                 SomeString=Replace(SomeString,"& amp ;","&")
  33.                 SomeString=Replace(SomeString,"& lt ;","<")
  34.                 SomeString=Replace(SomeString,"& gt ;",">")
  35.                
  36.         End If
  37.  
  38.  
  39.         ' Analyze the string for information in between <...> tags, and strip them all
  40.         For detempcounter=0 To Len(SomeString)
  41.                 If Mid(SomeString,detempcounter,1)="<" Then
  42.                         dehtmlmode=True
  43.                 ElseIf Mid(SomeString,detempcounter,1)=">" Then
  44.                         dehtmlmode=False
  45.                 ElseIf dehtmlmode=False Then
  46.                         ' Count non-HTML characters
  47.                         detempstring=detempstring+Mid(SomeString,detempcounter,1)
  48.                 End If
  49.         Next
  50.  
  51.         Return detempString
  52. End Function


Comments :


xlsior(Posted 1+ years ago)

 NOTE: The web forum messed up some of the HTML codes when I posted this program, so I have to fudge it a little.the 'replace' commands list some HTML-codes -- these are shown with additional spaces in the code above. Make sure to replace them with the proper values (without the spaces, like: &amp; )Likewise, the double quotes in the sample string should say &quot; to show the effect.Remove the spaces for the program to work properly!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal