使用json_encode获取中文unicode的问题
简单的数据库查询程序,数据库表里的中文保存的是中文unicode编码,例如中国在库里显示的是\u3537\u3456这样,
接收输入的中文是utf-8, 使用json_encode的自动中文转码功能可以获得unicode,可是问题来了,如果从转码后的字符串里获取
unicode呢,请大家帮忙给点思路啊?
例子代码如下
$str_input = "go南京trip"; //待查询的字符串
$str_input = json_encode($str_input);
$str_query = "go\u3537\3642trip";目标字符串
如何从$str_input获取$str_query呢?
回复讨论(解决方案)
什么意思?要把 go\u3537\3642trip 变为 go南京trip ?
http://www.welefen.com/php-unicode-to-utf8.html
,看来我没表达清楚,
我需要的是"go\u3537\3642trip", 即一旦有中文,就将它转化为其对应unicode的字符串,其他字符不变
你是没有说清楚!
1、\u3537\u3456 怎么会是 中国 呢?
你是怎么放进去的?请说一下
2、
$str_input = "go南京trip"; //一定要是 utf-8 的$str_input = iconv('gbk', 'utf-8', $str_input); //不然要转化成 utf-8 的echo json_encode($str_input);//得到 "go\u5357\u4eactrip"$str_query = trim(json_encode($str_input), '"'); //所以这样就得到了
呵呵,unicode代码是瞎编的,抱歉,楼上这位兄弟跟我目前的做法是一样的,但是此时
$str_query是json字符串,{"0":"go\u5357\u4eac\u5730\u94c1trip"}, 我只想要"go\u5357\u4eac\u5730\u94c1trip"。
用json_decode也不行,这时候应该怎么办呢,只能去字符串比较剥出来么?
那你的 $str_input 是数组,不是字符串
你取出值来不就行了?
$str_input = current($str_input);
$str_query = trim(json_encode($str_input), '"');
$str_input = "go南京trip";
$str_query = trim(json_encode($str_input), '"');
echo $str_query;
//得到{"0":"go\u5357\u4eac\u5730\u94c1trip"}
怎么才能从json结构里{"0":"go\u5357\u4eac\u5730\u94c1trip"}获得“go\u5357\u4eac\u5730\u94c1trip”呢
你为啥这样保存呢?DB以utf-8编码不就OK了?
$str_input = "go南京trip";$str_query = trim(json_encode($str_input), '"'); echo $str_query;
你为啥这样保存呢?DB以utf-8编码不就OK了?
数据库是不能更改的,表已经存在了,中文数据是以unicode保存的
$str_input = "go南京trip";$str_query = trim(json_encode($str_input), '"'); echo $str_query;
如果我直接echo,浏览器显示是空,直接放到sql句子里面发现是带{ }的json数据格式
贴出来看看!
$str_input = "go南京trip";
$str_query = trim(json_encode($str_input), '"');
echo "unicode: ", $str_query;
?>
chrome里面输出 unicode:
终于搞定了,多谢xuzuning

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo
