简体   繁体   中英

Handling UTF-8 string in PHP 5.3

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.

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

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

use this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM