Page 1 of 1

replace \uxxxx with &#xxxx;

Posted: Mon Nov 29, 2010 5:27 am
by RussellEngland
I've got a json feed that has unicode characters encoded with \u

I'd like to convert them to &# ;

I've tried this but get an error "Warning: preg_replace() [function.preg-replace]: Compilation failed: PCRE does not support \L, \l, \N, \U, or \u at offset 1 in testutf8.php on line 2"

Code: Select all

$utf8str = 'Renal Dialysis Nurses \u2013 Corby';
echo preg_replace("/\u([0-9a-f]{3,4})/i","&#x\\1;",$utf8str);
Any ideas?

Re: replace \uxxxx with &#xxxx;

Posted: Mon Nov 29, 2010 6:54 am
by Weirdan
You need to escape backslash in the regexp:

Code: Select all

preg_replace("/\\\\u([0-9a-f]{3,4})/i","&#x\\1;",$utf8str);

Re: replace \uxxxx with &#xxxx;

Posted: Mon Nov 29, 2010 11:17 am
by RussellEngland
Brilliant!!! Thank you :)