Written by

System Analyst at First Line Software
Question Flávio Lúcio Naves Júnior · Aug 18, 2021

How convert a String to HTML Entity Codes?

Good morning everybody,

In Html sometimes we need to use entities code for don't have problems with browsers all over the world. Ex. 'á' is converted to code: 'á' / 'é'  is converted to 'é'

I have a question, how can i convert a string like my name to this type of code? Ex. "Flávio" will be "Flávio". I tried this way:

USER>write $zconvert("Flávio","I","HTML")
Flávio

but don't worked :(
 

Best Regards.

Product version: Caché 2018.1

Comments

Marc Mundt · Aug 18, 2021

A couple of things...

You'll want to use "O" (output) mode instead of "I" (input) mode:
write $zconvert("Flávio","O","HTML")

However, I see that this still doesn't replace the "á". But if I add an "&" it replaces it with "&" as expected:
write $zconvert("Flávio&","O","HTML")
The table at the bottom of this section in the documentation lists which characters are encoded into entities in HTML mode.

0
Jeffrey Drumm  Aug 18, 2021 to Marc Mundt

Interesting that it works in the opposite direction:

USER> w $zconvert("Flávio","I","HTML")
Flávio
0
Julius Kavay  Aug 18, 2021 to Flávio Lúcio Naves Júnior

I think, the $zconvert() function will cover only the necessary entities. But you can use a simple method to convert characters to currently known(*) entities.

ClassMethod ToHTML(str)
{
   for i=$length(str):-1:1 set c=$ascii(str,i) set:$data(^entityChars(0,c),c) $extract(str,i)=c
   quit str
}

ClassMethod FromHTML(str)
{
   set i=0
   while $locate(str,"&[A-Za-z]+;",i,j,v) {
   set:$data(^entityChars(1,v),c) s=$length(v), $extract(str,j-s,j-1)=$c(c), j=j-s+1
   set i=j
   }
   quit str
}

I have a table (the ^entityChars() global) which contains more the 1400 entities. You can download the above class, together with the table from my FTP server (File: DC.Entity.xml):

Adr: ftp.kavay.at
Usr: dcmember
Psw: member-of-DC

A sample output:

USER>write ##class(DC.Entity).ToHTML("Flávio Lúcio Naves Júnior")
Flávio Lúcio Naves Júnior
USER>write ##class(DC.Entity).FromHTML("Flávio Lúcio Naves Júnior")
Flávio Lúcio Naves Júnior

(*) Currently known, because (1) I do not have all the currently known entities in my table and (2) with each new day, the W3C and the Unicode consortium can extend the current entity list.

0
Flávio Lúcio Naves Júnior  Aug 19, 2021 to Julius Kavay

Thanks, I'm trying to access your ftp but i'm not getting. Can you import the file here in the answer?

0
Julius Kavay  Aug 19, 2021 to Flávio Lúcio Naves Júnior

You have to use the login data (as specified, this is not a anonymous FTP). On Windows: command line (cmd.exe), on Linux: terminal. Then (on both systems): ftp ftp.kavay.at, enter username and passwprd, then: get DC.Entity.xml.

Do you really want to see some 1470 lines of code here?

0
Dmitry Maslennikov  Aug 19, 2021 to Julius Kavay

Why not to place it to some GitHub repo or at least gist?

0
Vitaliy Serdtsev · Aug 20, 2021

Why not use numeric codes?

$ascii("á") = 225set s1=$zconvert("Flávio","I","HTML"),
    s2=$zconvert("Flávio","I","HTML")
   
write s1,$select(s1=s2:" = ",1:" <> "),s2
0