How to decode a base64 string and select input charset using php? - php

How to decode a base64 string and select input charset using php?
For example it works correctly here : https://www.base64decode.org
I want decode a base64 string using php and I want select Windows-1252 charset.
I use this code:
base64_decode($encode);
But it doesn't show Windows-1252 charset!
Can anyone help me?

You can decode with the base64_decode($encode);, but then you can use the iconv() to convert the character set to the one you want. You have to know what character set the $encode data is using first.
For example if the $encode data was using utf-8 the use the following:
$decoded = base64_decode($encode);
$text = iconv("UTF-8", "ISO-8859-1", $decoded);
More information: http://php.net/manual/en/function.iconv.php

Related

Php json_encode converts utf8 string to characters codes

I have a Persian text "سرما"
And then when I convert it to JSON using json_encode(), I get a series of escaped character codes such as \u0633 which seems to be expected and of a rational process. But my confusion lies where I don't know how to convert them back into readable string of characters. How should I do that in PHP?
Should I use anything of mb_* family? I also have checked json_encode() parameters and have found nothing appropriate for me.
UPDATE
what I get saved in my DB is:
["u0633u0631u0645u0627"]
Which shows the characters are not escaped properly. While if I change it to
["\u0633\u0631\u0645\u0627"] it becomes easily readable by json_decode()
They should be converted back on the other end when it's decoded. This is the safest option as it might not be possible to guaranteed that the transmission or storage will not corrupt a multi-byte encoding.
If you're certain that everything is safe for UTF8 end-to-end you can do:
$res = json_encode($foo, \JSON_UNESCAPED_UNICODE);
http://php.net/manual/en/function.json-encode.php
Maybe try encoding the unicode characters, and then json_encoding it, then on the other side (receiving JSON) decode the json, then decode the unicode.
Example:
//Encode
json_encode(utf8_encode($string));
//Decode
utf8_decode(json_decode($string));
its simple just use JSON_UNESCAPED_SLASHES atribute
your problem is't utf8 you need force JSON to don't escape Slashes
example
$bar = "سرما";
$res = json_encode($bar, JSON_UNESCAPED_SLASHES );
// $res equal to ["\u0633\u0631\u0645\u0627"]
if you check the result in your MYSQL Database
it happen when you did't Use addslashes()
example
$bar = "سرما";
$res = json_encode($bar, JSON_UNESCAPED_SLASHES );
$res = addslashes($res);
// $res equal to ["\\u0633\\u0631\\u0645\\u0627"] now it's ready to use in MYSQL

PHP decode UTF-8 in URL ie &title=%C5%8Cyu to Ōyu not ÅŒyu

Please can you help me decode this URL so that it displays properly using PHP to output
This is the link
http://www.megalithic.co.uk/visits.php?op=site&sid=18341&title=Ōyu
I think it's actually coming through as UTF-8 - ie
&title=%C5%8Cyu
$title displays as ÅŒyu
How do I convert this in PHP? I need to use ISO-8859-1 on the page
None of these work
$title=iconv("UTF-8","ISO-8859-1",$title);
$title=iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $title);
$title = utf8_decode($title);
$title = urldecode($title);
Do I need to use the Multibyte MB extension and if so how?
Many thanks in advance
Andy
If that link is to your PHP page, and you get the value via $_GET['title'], then it's already decoded from the URL encoding and $_GET['title'] holds a UTF-8 encoded string with the character Ō. This character cannot be encoded in ISO-8859-1. If that is a strict requirement, you'll have to encode the character as HTML entity in order to express it in a strictly ISO-8859-1 encoded page:
echo htmlentities('Ō', ENT_COMPAT | ENT_HTML5, 'UTF-8');
The character "Ō" is not there in ISO-8859-1, so it is not possible to convert it from UTF-8 with any of the standard charset conversion functions.
It might, however, be possible to write a function that converts to numerical HTML encodings, like Ō for "Ō".

json_decode & file_get_contents doesn't get the UTF8 characters

I use
$link = json_decode(file_get_contents("http://graph.facebook.com/111866602162732"));
the result on that page shows:
"name": "L\u00e9ry, Quebec",
I then want to convert that with the accents.. like this:
$location_name = $link->name;
echo 'NAME ORIGINAL: '.$location_name;
$location_name = preg_replace('/\\\\u([0-9a-fA-F]{4})/', '&#x\1;', $location_name); // convert to UTF8
echo ' NAME after: '.$location_name;
I get the following result:
NAME ORIGINAL: Léry, Quebec NAME after: Léry, Quebec
my preg_replace is correct, so it's the original name that is being transformed by the file_get_contents.
If file_get_contents don't give you back a well format UTF-8 text, then json_decode you would return NULL. Json MUST be in UTF-8 encoding.
This function only works with UTF-8 encoded strings. (json_decode)
So, I guess that you're reading the data with another encoding. Check it out.
Most likely, you're treating a valid UTF-8 output given to you by json_decode as ISO-8859-1
See here, for example: http://www.i18nqa.com/debug/bug-utf-8-latin1.html
Make sure that you're treating your debug output as UTF-8 - that should solve the problem.

Change encoding (iconv in php) doesn't work

I have a database with data in windows-1253 encoding.
I'm trying to convert them to utf8 with iconv function and display them in a page but I get characters like these: g óôçí åðüìåíç ôáéíßá ôïõ
Any thoughts?
This is the code I use
iconv(mb_detect_encoding($this->row["question"], mb_detect_order(), true),"UTF-8",htmlentities(stripslashes($this->row["question"])))
If you know the encoding is windows-1253, then simply try to use:
iconv('Windows-1253','UTF-8', $text);

Convert a JSON into a UTF-8 string

I want to convert a JSON object into a string. when I am using json_encode I get a string but all with hex letters. I want to convert it to a UTF-8. In other words I want to see the characters. How do I do it?
I was using json_encode to store data such as Arabic Characters in MySQL fields.
It would store the Arabic characters as HEX within the Database like this:
u0644 u063a...
Which is incorrect. You must ensure that you wrap your json_encode with mysql_escape_string().
This will make sure that the data is put in MySQL as:
\u0644\u063a...
Then, when you use json_decode, it converts the HEX strings into UTF-8 and is output correctly.
You can try passing an option to json_encode():
json_encode ( $value, JSON_UNESCAPED_UNICODE );
The JSON_UNESCAPED_UNICODE option is only available in PHP version 5.4.0 and later.
Thanks.
You can't, in PHP. Besides, the strings will still be the same once you decode them.
you are looking exactly for the funcition json_decode
it can convert json strings into utf8
here is an example of arabic word
$re = json_encode('لغة عربية');
echo $re ;
$dd = json_decode($re);
echo $dd ;
die;
it output :
"\u0644\u063a\u0629 \u0639\u0631\u0628\u064a\u0629"
لغة عربية
more examples here
http://php.net/manual/en/function.json-decode.php

Categories