Keith John Hutchison
Keith John Hutchison

Reputation: 5287

What is the best way to replace unicode characters in a php string?

I have some strange characters showing up in a production database. The string I want to replace is \u00fc\u00be\u008c\u00a3\u00a4\u00bc.

This fails.

$column = str_replace('\u00fc\u00be\u008c\u00a3\u00a4\u00bc', "'", $column);

and this works.

$column = str_replace('ü¾Œ£¤¼',"'",$column) ;

What is the best way to replace unicode characters in a PHP string without copying in the decoded text?

Upvotes: 4

Views: 21703

Answers (1)

Keith John Hutchison
Keith John Hutchison

Reputation: 5287

After following the lead from https://stackoverflow.com/users/395384/epb I used json_decode to translate the unicode which works.

$unicode = json_decode("\u00fc\u00be\u008c\u00a3\u00a4\u00bc") ;
$column = str_replace($unicode, "'", urldecode($row[$columnIndex]));

Upvotes: 9

Related Questions