replace \uxxxx with &#xxxx;

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
RussellEngland
Forum Newbie
Posts: 8
Joined: Thu Nov 11, 2010 3:58 pm

replace \uxxxx with &#xxxx;

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: replace \uxxxx with &#xxxx;

Post 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);
RussellEngland
Forum Newbie
Posts: 8
Joined: Thu Nov 11, 2010 3:58 pm

Re: replace \uxxxx with &#xxxx;

Post by RussellEngland »

Brilliant!!! Thank you :)
Post Reply