[英]How convert a String to a String with HTML entities?
我正在寻找一种方法,最好是在python
,但PHP
也是可以的,甚至是在线站点,也可以将字符串转换为
"Wählen"
变成像
"Wählen"
也就是说,将每个ISO 8859-1字符/符号替换为其HTML实体。
echo htmlentities('Wählen', 0, 'utf-8');
^ PHP
PS : 根据需要在哪里显示编码的字符串来学习参数 :
// does not encode quotes
echo htmlentities('"Wählen"', 0, 'utf-8');
// encodes quotes
echo htmlentities('"Wählen"', ENT_QUOTES, 'utf-8');
像这样
$html="Wählen";
$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'ISO-8859-1');
// OR $html = htmlentities($html, ENT_COMPAT, 'ISO-8859-1');
echo $new = htmlspecialchars($html, ENT_QUOTES);
对于Python3
>>> import html.entities
>>> reventities = {k:'&'+v+';' for v,k in html.entities.entitydefs.items()}
>>> "".join(reventities.get(i, i) for i in "Wählen")
'Wählen'
另一种(可能更快)的方式
>>> toentity = {k: '&'+v+';' for k,v in html.entities.codepoint2name.items()}
>>> "Wählen".translate(toentity)
'Wählen'
蟒蛇:
# -*- coding: utf-8 -*-
from htmlentitydefs import codepoint2name
def uni_to_html(s):
new_s = ""
for c in s:
try:
new_s += '&{};'.format(codepoint2name[ord(c)])
except KeyError:
new_s += c
return new_s
print uni_to_html(u"Wählen") # Wählen
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.