Search code examples
phpencodingutf-8ratchet

How to set encoding text to UTF-8 in Ratchet (PHP, WebSocket)?


I have the app based in Ratchet. Script created in UTF-8 encoding.

<?php

namespace MyApp;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use MyApp\Player;
use MyApp\User;
use MyApp\Computer;
use MyApp\Room;
use MyApp\Utils;
use MyApp\PdoConnection;

class Chat implements MessageComponentInterface {
...

In onMessage I accept message from user

public function onMessage(ConnectionInterface $from, $msg) {

When $msg is english text it's all ok, but if it's russian text for example it's look very bad. I understand that encoding is wrong, but how to set encoding to UTF-8 in WebSocket script?


Solution

  • I've understood. WebSocket and Ratchet works well with encoding. Point is that my console where I start websocket script was in bad encoding, I change that. In addition I use substr and other functions, but it's necessary use mb_* in my case.