PHP UTF-16转换ASCII码 [英] PHP UTF-16 to ASCII conversion
问题描述
String:Carl Sagan '
除非我误解了这个问题,'
不是UTF-16的问题。该字符串已经运行了 htmlspecialchars()
或 htmlentities()
,单引号转换为html实体代表'
。
要恢复正常,您需要执行 html_entity_decode()。
通常你只想做html编码在输出的时候,你避免持续这种转换。如果您在某处获取HTML输入以获取这些字符串,则可能需要在执行UTF转换和存储之前解码实体。
Consider the following string. Its encoded in UTF-16-LE and saved into a PHP variable. I failed to get either mbstring or iconv to replace the ' with single quote. What would be a good way to sanatize it.
String : Carl Sagan'
s Cosmic Connection
Unless I'm misunderstanding the question, '
isn't a UTF-16 issue. That string has had htmlspecialchars()
or htmlentities()
run on it, and the single quote was converted to the html entity represenation '
.
To get it back to normal you need to do an html_entity_decode().
Generally you only want to do html encoding at output time so you avoid persisting that kind of conversion. If you are taking in HTML input somewhere to get these kind of strings, you probably want to decode entities before you do the UTF conversion and storage.
这篇关于PHP UTF-16转换ASCII码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!