Answer the question
In order to leave comments, you need to log in
How to solve problems with preg_replace encoding and UTF-8?
Ran this with PHP 5.3.13 on Windows and PHP 5.3.10 on Ubuntu.
In Chrome 33.0.1750.154 and FF 26.0 browsers that correctly detected the encoding as UTF-8
<?php
header('Content-Type: text/html; charset=UTF-8');
$message = "вае№\n_п8bс!\n ии";
$message = preg_replace('/[^a-z0-9а-я\!]*/i', '', $message);
echo $message;
vaep8b�!ii
Answer the question
In order to leave comments, you need to log in
You have a document in UTF-8 and output it in UTF-8, and what is the encoding of the $message string?
Convert it to utf-8 using iconv before preg_replace.
The fact that browsers displayed your text does not mean that it is in UTF-8, they automatically picked up the encoding and ignored your Content-Type: text/html; charset=UTF-8
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question