4

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?

Keith John Hutchison
  • 4,955
  • 11
  • 46
  • 64
  • this is just a guess, since I have nowhere to test this atm, but try using double quotes within your first example. Single quotes just treat that as literal text. – Auris Nov 25 '16 at 11:05
  • 2
    You should look at the answers on http://stackoverflow.com/questions/6058394/unicode-character-in-php-string [Edit: The question deals with a single character, but the same principles apply to a string of them.] – EPB Nov 25 '16 at 11:34
  • Thanks @EPB. using json_decode() worked. – Keith John Hutchison Nov 25 '16 at 21:15

1 Answers1

7

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]));
Community
  • 1
  • 1
Keith John Hutchison
  • 4,955
  • 11
  • 46
  • 64
  • Apparently, this doesn't work anymore. Running your exact code, `json_decode` returns `null` and `str_replace` throws a Fatal Error because of that. – Klaassiek Dec 14 '22 at 22:19
  • 1
    This answer is over 6 years old. The version of php at the time was php5. Added php-5.5 tag to the question. Removed php tag from the question. – Keith John Hutchison Dec 15 '22 at 03:55