PHP UTF-16转换ASCII码 [英] PHP UTF-16 to ASCII conversion

查看:431
本文介绍了PHP UTF-16转换ASCII码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下字符串。它以UTF-16-LE编码并保存到PHP变量中。我没有得到mbstring或iconv替换'与单引号。什么是一个很好的方法来消除它。



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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆