简体   繁体   中英

php decode encoded string

I have to save russian language product description in db. So for that I converted that string to utf 8 using below code,

$data = 'Это русский';
$cData = iconv(mb_detect_encoding($data, mb_detect_order(), true), "UTF-8", $data);

It is working fine. But I need to get that data back, and I don't know how to decode it again. I tried below one, but it is not working,

$des = $object->getDescription("ru");
$enc = mb_detect_encoding($des, "UTF-8,ISO-8859-1");
echo iconv($enc, "UTF-8", $des);

and I tried below one, but not working

utf8_decode ( $data );

Can any one tell me how to decode this ?

Update:

I tried below one to encode,

$data = 'Это русский';
$cData =  htmlentities($data, ENT_COMPAT, 'UTF-8');

It's working fine, But how to decode this ?

I tried below one, but it is not working..

$des = $object->getDescription("ru");

 echo $cData = htmlentities($des, ENT_COMPAT, 'UTF-8');

The encoding appears to be Windows-1251. Encode to UFT-8 using:

$html_utf8 = mb_convert_encoding($html, "utf-8", "windows-1251");

Decode back to Windows-1251 using:

$html_1251 = mb_convert_encoding($html, "windows-1251", "utf-8");

PHP has a UTF-8 decode function, utf8_decode()

utf8_decode ( $data );

From the manual :

This function decodes data, assumed to be UTF-8 encoded, to ISO-8859-1.

What is your original encoding?

In your decoding example you're not converting back to your original encoding, but again to UTF-8. Try this:

$original_encoding = '...'; //put your original encoding here
$description = $object->getDescription("ru");
echo iconv('UTF-8', $original_encoding, $description);

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