Convert Html code

Just starting out? Need help? Post your questions and find answers here.
Wolf
Enthusiast
Enthusiast
Posts: 234
Joined: Sat Apr 03, 2004 12:00 pm
Location: S.T

Convert Html code

Post by Wolf »

Hello everyone

How can i convert Html code like "۱&#1779" to clear text?

I get this html code when run this code:

Code: Select all

  InitNetwork()

  *Buffer = ReceiveHTTPMemory("http://zartools.com/zar/tools/time-date/date-f.php")
  If *Buffer
    Size = MemorySize(*Buffer)
    Debug "Content: " + PeekS(*Buffer, Size, #PB_UTF8|#PB_ByteLength)
    FreeMemory(*Buffer)
  EndIf
When you open above URL in your browser you see clear result in page but when run above code in PB we get result in Html code like:

Code: Select all

&#1670;&#1607;&#1575;&#1585;&#1588;&#1606;&#1576;&#1607; &#1777;&#1779; &#1588;&#1607;&#1585;&#1610;&#1608;&#1585; &#1777;&#1779;&#1785;&#1784;</a></div>")
Last edited by Wolf on Mon Sep 16, 2019 11:41 am, edited 2 times in total.
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Convert Html code

Post by infratec »

Code: Select all

Procedure.s Decode(Text$)
  
  Protected Length.i, Result$, Help$, Value$
  
  Length = Len(Text$)
  
  For i = 1 To Length
    Char$ = Mid(Text$, i, 1)
    If Char$ = "&"
      Help$ = ""
      Value$ = ""
      While Help$ <> ";"
        i + 1
        Help$ = Mid(Text$, i, 1)
        If Help$ <> "#" And Help$ <> ";"
          Value$ + Help$
        EndIf
      Wend
      Result$ + Chr(Val(Value$))
    Else
      Result$ + Char$
    EndIf
  Next i
  
  ProcedureReturn Result$
  
EndProcedure


InitNetwork()

*Buffer = ReceiveHTTPMemory("http://1abzar.ir/abzar/tools/time-date/date-fa.php")
If *Buffer
  Size = MemorySize(*Buffer)
  Content$ = PeekS(*Buffer, Size, #PB_UTF8|#PB_ByteLength)
  FreeMemory(*Buffer)
  Pos1 = FindString(Content$, "#'>")
  If Pos1
    Pos1 + 3
    Pos2 = FindString(Content$, "<", Pos1)
    If Pos2
      Text$ = Mid(Content$, Pos1, Pos2 - Pos1)
      ;Debug Text$
      Debug Decode(Text$)
    EndIf
  EndIf
EndIf
Wolf
Enthusiast
Enthusiast
Posts: 234
Joined: Sat Apr 03, 2004 12:00 pm
Location: S.T

Re: Convert Html code

Post by Wolf »

Thank you so much infratec :D
Very fast and helpful.
Post Reply