简体   繁体   English

如何将utf-8字符串解码为实际文本?

[英]how to decode utf-8 string to actual text?

I am using codeigniter with php 5.6 and mysql db for this specific purpose. 我为此使用php 5.6和mysql db的codeigniter。 I want to store strings from different languages to database and retrieve them later. 我想将不同语言的字符串存储到数据库中,以后再检索。 Problem I am facing here is when I am trying to grab the value, instead of giving out exact text, it shows something in the following format. 我在这里面临的问题是,当我尝试获取值而不是给出确切的文本时,它以以下格式显示内容。

%E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87 %E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87

Here's some test code 这是一些测试代码

function charset($get = NULL){
   $direct = "नमस्ते";

$this->load->database();    
  if($get != NULL){
        $this->db->insert('test', array("name"=>"get", "value"=>$get));
        $this->db->insert('test', array("name"=>"direct", "value"=>$direct));
    }
    $results = $this->db->get('test')->result_array();

    echo "<pre>";
    print_r($results);

}

and Here are the results 这是结果

Array
(
    [0] => Array
    (
        [id] => 16
        [name] => get
        [value] => %E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87
    )

[1] => Array
    (
        [id] => 17
        [name] => direct
        [value] => नमस्ते
    )


)

how would you get (or convert) the exact word instead of ut8 encoded string? 您将如何获取(或转换)确切的单词而不是ut8编码的字符串?

I have checked your code like below and it is returning value proper: 我已经检查了您的代码,如下所示,它返回的值正确:

$variable = '%E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87';
echo urldecode($variable);

Hope I understood your code properly. 希望我能正确理解您的代码。

There are multiple things at play here 这里有很多事情在起作用

MySQL 的MySQL

In MySQL you need to configure the connection/client to use it: 在MySQL中,您需要配置连接/客户端以使用它:

SET NAMES 'utf8'

In PHP it might be something similar to this: 在PHP中,可能与此类似:

$this->db->query("SET NAMES 'utf8'");

HTML 的HTML

When outputting to HTML, you'll also have to tell the client (browser) what data you're passing: 当输出为HTML时,您还必须告诉客户端(浏览器)要传递的数据:

header('Content-Type: text/html; charset=utf-8');

And/or in your HTML body: 和/或在您的HTML正文中:

<html>
    <head>
        <meta charset="utf-8" />

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

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