Skip to content

Instantly share code, notes, and snippets.

@munsiwoo
Last active December 17, 2018 14:05
Show Gist options
  • Save munsiwoo/65f6780ef4ac75f4786ae0535cb4dc7e to your computer and use it in GitHub Desktop.
Save munsiwoo/65f6780ef4ac75f4786ae0535cb4dc7e to your computer and use it in GitHub Desktop.
HTML Entities en/decoder
import urllib.parse
# made by munsiwoo
# <img src=# onerror="&#97;&#108;&#101;&#114;&#116;&#40;&#49;&#41;">
def htmlentities_encode(string, url_encoding=0) :
result = str()
for st in string :
result += '&#'+str(ord(st))+';'
if url_encoding :
return urllib.parse.quote(result)
return result
def htmlentities_decode(string) :
string = string.replace('&#', ' ')
string = string.replace(';', '').strip()
result = str()
for st in string.split(' ') :
result += chr(int(st))
return result
print(htmlentities_encode('abcd'))
print(htmlentities_encode('abcd', 1))
print(htmlentities_decode('&#97;&#98;&#99;&#100;'))
'''
&#97;&#98;&#99;&#100;
%26%2397%3B%26%2398%3B%26%2399%3B%26%23100%3B
abcd
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment