From the PHP manual:
Single and double quoted PHP strings have special meaning of backslash. Thus if
\
has to be matched with a regular expression\\
, then"\\\\"
or'\\\\'
must be used in PHP code.
First of all, in your regular expression, you're only using one backslash (\
). As explained in the PHP manual, you need to use \\\\
to match a literal backslash (with some exceptions).
Second, you are missing the capturing groups in your original expression. preg_replace()
searches the given string for matches to the supplied pattern and returns the string where the contents matched by the capturing groups are replaced with the replacement string.
The updated regular expression with proper escaping and correct capturing groups would look like:
$str2 = preg_replace('/\\\\u([0-9a-f]+)/i', '&#x$1;', $str);
Output:
おはよう
Expression: \\\\u([0-9a-f]+)
\\\\
- matches a literal backslashu
- matches the literalu
character(
- beginning of the capturing group[0-9a-f]
- character class -- matches a digit (0
-9
) or an alphabet (froma
-f
) one or more times
)
- end of capturing groupi
modifier - used for case-insensitive matching
Replacement: &#x$1
&
- literal ampersand character (&
)#
- literal pound character (#
)x
- literal characterx
$1
- contents of the first capturing group -- in this case, the strings of the form304a
etc.