2

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');

3 Answers 3

5

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");
Sign up to request clarification or add additional context in comments.

1 Comment

Okay, I will check again.
1

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.

Comments

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);

1 Comment

iconv(mb_detect_encoding($data, mb_detect_order(), true), "UTF-8", $data); this is my encoding method..

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.