简体   繁体   English

用mb_convert_encoding将ASCII编码的unicode转换为PHP中的SQL语句

[英]ASCII encoded unicode to SQL statement in PHP with mb_convert_encoding

I'm working with a file type that is comprised of ASCII characters only. 我正在使用仅包含ASCII字符的文件类型。 Other characters are encoded. 其他字符已编码。 Two byte characters are preceded with \\X2\\ and followed by \\X0\\ and four byte characters use \\X4\\ and \\X0\\ . 两个字节字符以\\X2\\开头,后跟\\X0\\ ,四个字节字符使用\\X4\\\\X0\\ I can find these easily enough but I'm not sure how to then handle them. 我可以很容易地找到它们,但不确定如何处理它们。

eg the character ø is given as \\X2\\00F8\\X0\\ . 例如,字符ø被指定为\\X2\\00F8\\X0\\

My whole string I'm looking for is 100 mm\\X2\\00F8\\X0\\ . 我要寻找的整个字符串是100 mm\\X2\\00F8\\X0\\

How can I get that to a string that I could then say insert into my DB? 我怎样才能得到一个我可以说插入数据库的字符串?

I figure I need to use mb_convert_encoding() 我想我需要使用mb_convert_encoding()

But I'm not sure which encoding I want to go to and from. 但是我不确定我想使用哪种编码。

Any ideas? 有任何想法吗?

Put simply I have "100 mm\\X2\\00F8\\X0\\" and I want to output that as "100 mmø" 简而言之,我有“ 100 mm \\ X2 \\ 00F8 \\ X0 \\”,我想将其输出为“ 100mmø”

It depends on SQL server you are using. 这取决于您使用的SQL Server。 For example for MySQL you can use mysqli_real_escape_string 例如对于MySQL,您可以使用mysqli_real_escape_string

This method automatically takes encoding from your sql connection so it is safe to use. 此方法自动从您的sql连接中获取编码,因此可以安全使用。

I would pass raw value 100m ø. 我将通过原始值100mø。

so all I needed was hex2bin('00F8') 所以我只需要hex2bin('00F8')

Then utf8_encode(...) to get it from utf-16 to utf 8 然后utf8_encode(...)将其从utf-16升级到utf 8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM