0

I have a JSON string from an ajax call which contains some special character. The problem is, that there is a function that removes all slashes from the string (which I have to use sadly). But it breaks any Unicode character in it...

So I have a string like that:

mmu00b2

Which should be converted to UTF-8:

mm²

I tried this regex here, but with no luck: preg_replace("/u(\w+)/i", "\x$1", $str );

Game Unity
  • 125
  • 10

1 Answers1

1

'/u(\w+)/i' will match 'u00b2' since \w+ matches with any word characters including underscores.

You should use '\d+' instead to only matches the digits. This will give you a match of 'u00' and breaks from 'b' so you won't need to add $1 anymore on your replacement.

preg_replace("/u(\d+)/i", "\x", 'mmu00b2')

output: mm/xb2

  • 2
    Thank you for your help, however, this doesn't quite do what it should... So ended up by fixing the escaped Unicode by adding the slash `preg_replace('/u(\d.{3})/i', '/\1', $str)` to it back. Then you can just save it to the DB with `json_encode( $str, JSON_UNESCAPED_UNICODE ).` Also notice this answer here https://stackoverflow.com/a/30424859/7149996 – Game Unity Jan 11 '20 at 20:20