0

We are using some third-party php library functions and have some difficulties converting utf-8 strings.

After some experiment, this is what we got so far:

(1) The following will print the correct unicode word (it's 'one' word) in browser(we use Firefox):

$s = "\345\244\247";
echo $s;

大 <-- (prints out a correct unicode word)

(2) However, the library function will return something like this:

$s2 = "\\345\\244\\247";
echo $s2;

\345\244\247  <-- the print out will contain escape character so the unicode isn't showing correctly

(3) So the question is, is there a php function capable of doing this, converting $s2 to the correct unicode form (like $s)?

Thanks.

The environment is PHP 5.3.

1
  • If the only problem is the double \\ you can set $s2 = str_replace("\\","\",$s2); Commented Jan 14, 2013 at 9:14

2 Answers 2

1

Something like http://ideone.com/Owl2a3 :


function _conv($oct) {
    return chr(octdec($oct[1]));
}

$es = "\\345\\244\\247";
$es = preg_replace_callback('@\\\\(\d{3})@', '_conv', $es);

echo $es;

outputs

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

Comments

0

the problem is, that you're escaping the slashes!

use this:

$s2 = str_replace("\\","\",$s2);

1 Comment

That won't change anything. By that time, the string contains the text with the \ - it is no longer a literal that PHP interprets.

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.