mmmm
mmmm

Reputation: 3938

Fixing preg_replace PREG_BAD_UTF8_ERROR

I've read this - preg_replace PREG_BAD_UTF8_ERROR but the answer didn't really help me. I want to remove some of the unprintable characters from string. This is my code:

mb_internal_encoding("UTF-8");
mb_regex_encoding('UTF-8');
$string = "Cortège\x99 de gymnastique devant LL. MM. ęźżąśł";

echo mb_detect_encoding($string);
var_dump(mb_ereg_replace("/\\x99/u", '', $string));
var_dump(preg_last_error() == PREG_BAD_UTF8_ERROR);

Things I want to remove are for example "\x99". Output from given code is:

UTF-8

string 'Cortège de gymnastique devant LL. MM. ÄźşÄĹĹ' (length=52)

boolean true

How am I supposed to do it correctly?

Upvotes: 0

Views: 559

Answers (1)

Bizmate
Bizmate

Reputation: 1882

You dont need to use delimiters in mb_ereg_replace

when i use

echo 'encoding ' . mb_detect_encoding($string) . "\n\n<br/>";
echo 'replace result ' . mb_ereg_replace("\\x99", '', $string) . "\n\n<br/>";

I get

encoding UTF-8 
replace result Cortège de gymnastique devant LL. MM. ęźżąśł

\x99 is gone

Upvotes: 2

Related Questions