0

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
  • 2
    buffed clubbed grubbed muffed overdubbed persuaded puffed scrubbed stubbed stuffed handcuffed etc. Commented Oct 1, 2010 at 15:05
  • Silly (but correct) answer: Both languages are Turing Complete, so anything you can do in one, you can do in the other (from a high level standpoint at least). So instead of asking "Is it possible", you should probably ask "How can I"... Commented Oct 1, 2010 at 15:13

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.