I have a function in Java which I need to convert into php. It converts unicode text into characters. For example input is
“Every person haveue280 sumue340 ambition”;
I want to replace ue280, ue340 to \ue280, \ue340 with regular expression
Here is code written in Java
String in = "Every person haveue280 sumue340 ambition";
Pattern p = Pattern.compile("u(\\p{XDigit}{4})");
Matcher m = p.matcher(in);
StringBuffer buf = new StringBuffer();
while(m.find())
m.appendReplacement(buf, "" + (char) Integer.parseInt(m.group(1), 16));
m.appendTail(buf);
String out = buf.toString();
Is it possible to convert it into php
Thanks in advance
In PHP, use
preg_replace()for regex functionality.I don’t think PHP’s preg_* functions support the wordy {XDigit} notation, so you’ll need to use a character class like
[0-9A-Fa-f]for hex digits.If you know regex well enough to understand the code you already have then that should be enough to get you started.