dongzhuo1733 2014-07-29 21:41
浏览 91

来自cp437的utf8字符在通过JS呈现时出现乱码,而不是在呈现php时

I have stored text in my database as utf-8. The text may contain some ascii art from the code point U+2500 to U+25FF range in the utf-8 table, perhaps even other code point ranges. I'm not sure since this is user edited. The text is delivered to the client using JSON over REST, and somehow the text is distorted along the way.

The utf-8 character range I'm referring to here, is of a three byte nature. For instance, a set of 0xe2 0x96 0x93 would in utf-8 equal ▓ or as plain text ▓. Instead of displaying this single character, the client displays each individual byte as it's own character, which would end up displaying â–‘

Not sure how to attack this. I've tried to figure out a way to convert the text server side using php, but the utf-8 table is just to large for me, and checking for every potential three byte combination is just overwelming. This should be easy. I've seen this done before, but then the entire page was rendered server side. This is an angularJs page.

Can anybody give me pointers as to solve this problem? Thank you.

Edit: stripped down code (php rendered)

<head>
    <meta charset="utf-8">
    ...
</head>
<body>
    ...
    <?
        $link = new mysqli($mysql_host, $mysql_user, $mysql_pass, $mysql_db);
        $result = $link->query('select descr from my_table where id = 1');
    ?>
    <div class="well"><?=$result['descr']; ?></div>
    ...
</body>

Would display:

░░ ░░

  ▒░                                                                      ░▒
  ▓░                                                                      ░▓
  ▓░                                                                      ░▓
  █░                                                                      ░█
  █                                                                        █
  █▓                                                                      ▓█
  ▓▓▌                                                                    ▐▓▓
  ▀██▄                                                                  ▄██▀
 ▄ ▄████▓▓▓█▄█▀▀    ▀▀           ▄   ▄▄▄▌▄▓████████████████████████▓▀▓████▄ ▄

The same database table queried using Laravel and sent over http async (also largely stripped down code):

class MyModel extends Eloquent {
    public function getDescriptionAttribute($value) {
        return $value;
    }
}

class MyModelController extends Controller {
    public function getModel ($modelId) {
        return Response::json(MyModel::findOrFail($modelId));
    }
}

angular.module(_SERVICES_).factory('MyModelService', ['Restangular', function (Restangular) {
    'use strict';

    return {
        get: function (id) {
            return Restangular.one('model', id).get();
        }
    }
}

angular.module(_CONTROLLERS_).controller('MyModelCtrl', ['$scope', '$routeParams', 'MyModelService',
    function ($scope, $routeParams, MyModelService) {
        MyModelService.get($routeParams.modelId).then(function (response) {
            $scope.model = response;
        }
    }
}

<head>
    <meta charset="utf-8">
    ...
</head>
<body ng-controller="MyModelCtrl">
    ....
    <div class="well" ng-bind-html="model.descr"></div>
    ...
</body>

Would display

â–‘                                                                  
â–‘
â–‘â–‘                                                                      â–‘â–‘
â–’â–‘                                                                      â–‘â–’
â–’â–‘                                                                      â–‘â–’
â–“â–‘                                                                      â–‘â–“
â–“â–‘                                                                      â–‘â–“
█░                                                                      ░█
â–ˆ                                                                        â–ˆ
█▓                                                                      ▓█
â–“â–“â–Œ                                                                    â–â–“â–“
▀██▄                                                                  ▄██▀
▄ ▄████▓▓▓█▄█▀▀    ▀▀           ▄   ▄▄▄▌▄▓████████████████████████▓▀▓████▄ ▄

Edit2: Request/Response headers

PHP rendered page

Request Headers
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,nb;q=0.6,nn;q=0.4,no;q=0.2
Cache-Control: no-cache Connection:keep-alive
Host: example.com Pragma:no-cache
Referer: http://example.com/mymodel.php?id=1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36 

Response Headers
Cache-control: private
Connection: Keep-Alive
Content-Encoding: gzip 
Content-Type: text/html;charset=utf-8 
Date: Tue, 29 Jul 2014 20:18:10 GMT 
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive: timeout=5, max=100 
Pragma: no-cache Server:Apache/2.4.9 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4
Transfer-Encoding: chunked
Vary: Accept-Encoding,User-Agent 
X-Powered-By: PHP/5.4.28

Async request/response

Request Headers
Accept: application/json, text/plain, */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,nb;q=0.6,nn;q=0.4,no;q=0.2
Cache-Control: no-cache
Connection: keep-alive
Host: example.com
Pragma: no-cache
Referer: http://www.example.com/modelview/1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36

Response Headers
Cache-Control: max-age=0
Cache-Control: no-cache
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 3669
Content-Type: application/json;charset=utf-8
Date: Wed, 30 Jul 2014 12:29:11 GMT
Expires: Wed, 30 Jul 2014 12:29:11 GMT
Keep-Alive: timeout=5, max=96
Server: Apache/2.4.9 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4
Vary: Accept-Encoding,User-Agent
X-Frame-Options: SAMEORIGIN
X-Powered-By: PHP/5.4.28
X-UA-Compatible: IE=edge,chrome=1
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
    • ¥15 vs2019中数据导出问题
    • ¥20 云服务Linux系统TCP-MSS值修改?
    • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
    • ¥20 怎么在stm32门禁成品上增加查询记录功能
    • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
    • ¥50 NT4.0系统 STOP:0X0000007B
    • ¥15 想问一下stata17中这段代码哪里有问题呀
    • ¥15 flink cdc无法实时同步mysql数据
    • ¥100 有人会搭建GPT-J-6B框架吗?有偿