简体   繁体   English

PHP解码编码的字符串

[英]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, 因此,我使用以下代码将该字符串转换为utf 8,

$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. 编码似乎是Windows-1251。 Encode to UFT-8 using: 使用以下代码编码为UFT-8:

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

Decode back to Windows-1251 using: 使用以下命令解码回Windows-1251:

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

PHP has a UTF-8 decode function, utf8_decode() PHP具有UTF-8解码功能utf8_decode()

utf8_decode ( $data );

From the manual : 从手册:

This function decodes data, assumed to be UTF-8 encoded, to ISO-8859-1. 此函数将假定为UTF-8编码的数据解码为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. 在解码示例中,您并没有转换回原始编码,而是再次转换为UTF-8。 Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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