tags:

views:

41

answers:

2

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

+2  A: 

Yes it's possible. (I know I'm not posting a PHP method, but all you asked was if it's possible).

Edit: no need to choose this as the answer, but if someone wants an function converted just say so, don't ask if it's possible.

Viper_Sb
good Viper. You r right. Its possible
EarnWhileLearn
+2  A: 

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.

Spudley