简体   繁体   中英

Laravel 5.0 utf8 convert?

I am in trouble.

windows 8.1
xampp 3.2.1
php 5.4
Laravel 5.1
Lang utf8(Japanese)

MySql

character_set_client      utf8
character_set_connection  utf8
character_set_database    utf8
character_set_filesystem  binary
character_set_results     utf8
character_set_server      utf8
character_set_system      utf8
character_sets_dir        C:\xampp\mysql\share\charsets\

php.ini

extension=php_mbstring.dll 

mbstring.language = Japanese 
mbstring.internal_encoding = UTF-8 
mbstring.http_input = auto 
mbstring.http_output = UTF-8 
mbstring.encoding_translation = On 
mbstring.detect_order = auto 
mbstring.substitute_character = none; 
mbstring.func_overload = 0 

insert action

public function register(Request $request) {
$group = new Group;
$group->name = $request->name;
$group->remarks = $request->remarks;

$validator = $this->validator($request->all());
if ($validator->fails()) {
    $this->throwValidationException(
        $request, $validator
    );
}

$group->save();

return redirect()->guest('master/group');
}

view insert tag

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

In this source code, it will be insert without problems.

However, it is garbled when you update.

update action

public function update(Request $request) { 
$group = Group::find($request->id); 
$group->name = $request->name; 
$group->remarks = $request->remarks; 
$group->user_id = $request->user_id; 

$validator = $this->validator($request->all()); 
if ($validator->fails()) { 
    $this->throwValidationException( 
        $request, $validator 
    ); 
} 

$group->save(); 

return redirect()->guest('master/group'); 
} 

I tried several.

  1. var_dump($request->name); // NG

  2. var_dump(utf8_encode($request->name)); // NG

  3. var_dump(utf8_decode($request->name)); // OK

When you update has caused the question to whether a correct code to carry out the utf8_decode () .

What, what's wrong is setting?

Do not use any encoders/decoders.

Instead of the var_dump() , do SELECT col, HEX(col) FROM ... WHERE ... and show us the output. The hex should mostly be groups of 6 hex digits: E8xxyy . (Space is 20 .)

If you don't get hex like that, then the data was not stored correctly. Otherwise, the problem is on the output.

To see the hex in PHP:

$hex = unpack('H*', $text);
echo implode('', $hex);

Again look for lots of E8xxyy . This will further narrow down where the problem is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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