Android PHP Base64 decode with different results - php

I have a big problem : I don't have the same result if I do base64_decode($string); in php or if I do Base64.decode(string); in android.
Example :
with this string : WWhiZWWSZpNlaGSTnpljZQ==
In php, result is Yhbee’f“ehd“ž™ce .
In android, the result is Yhbee�f�ehd���ce
I think there is an encoding problem, but I don't know where, the output of my PHP server is ISO-8859-1, I don't find how to tell to Base64.decode to use this type of encoding.
Can you help me please. Thx for answers.
PS : I can't touch the PHP script.

I think this will help you, Don't use .toString() use this instead
var str = "WWhiZWWSZpNlaGSTnpljZQ=="
var decoded = String(Base64.decode(str, Base64.NO_WRAP))
I know this to late to answer :-D

You should try this:
Base64.decode(content.getBytes("ISO-8859-1"), Base64.DEFAULT);
By this You convert the input String content to ISO-8859-1 encoded byte stream that will be decoded from base64.

I think the decode is fine, but Android isn't applying the right encoding (probably utf-8).
Does this content appear on an HTML page? If so, are you properly enforcing the ISO-8859-1 encoding?
Alternatively, you could also switch to UTF-8.

Related

Decoding strings on PHP: Data treats UTF-8 Bytes as Windows-1252

I am getting data from a web API which has a strange encoding. I am using PHP and can't seem to decode input strings. I seem to be having this problem, which explains what's going but doesn't really help me figure out how to fix it.
Can anyone help?
Thanks!
You may want to try analyzing the encoding using something like mb_detect_encoding().
http://www.php.net/mb_detect_encoding
You can use mb_detect_encoding() to detect the encoding of the strings.
If they are not what you are expecting, you can use mb_convert_encoding() to convert to something like UTF-8 or whatever you want.

How do I decode the following string in PHP?

Does anyone know how to properly decode the following string in PHP?
=?iso-8859-1?Q?OLG=20Slots=20at=20Windsor=20=26=20Caesars=20Windsor=20w?=
I tried using
quoted_printable_decode()
but did not produce the desired result.
This string retrieved from an email header. This above string is the "Subject". It appears that email clients (both web-based and applications) are able to decode the string properly.
Thanks for your time!
It is not url_encoded, instead try this :
$subject = '=?iso-8859-1?Q?OLG=20Slots=20at=20Windsor=20=26=20Caesars=20Windsor=20w?=';
echo utf8_decode(imap_utf8($subject));
Manual
The first bit suggests it's encoded in ISO-8859-1, which is, if I'm reading Wikipedia correctly, standard ASCII.
This means that you probably don't need to decode the string, you just need to understand it ;-)
Where did you find it? What do you think it's meaning might be? (eg is it submitted form data, some sort of RPC encoding, something else?)
Thank you! Just seen your edit. Have you tried b64 decoding it? At a guess it's base64 encoded ASCII. Try base64_decode().

How to parse unicode format (e.g. \u201c, \u2014) using PHP

I am pulling data from the Facebook graph which has characters encoded like so: \u2014 and \u2014
Is there a function to convert those characters into HTML? i.e \u2014 -> —
If you have some further reading on these character codes), or suggested reading about unicode in general I would appreciate it. This is so confusing to me. I don't know what to call these codes... I guess unicode, but unicode seems to mean a whole lot of things.
that's not entirely true bobince.
How do you handle json containing spanish accents?
there are 2 problems.
I make FB.api(url, function(response)
... var s=JSON.stringify(response);
and pass it to a php script via $.post
First I get a truncated string. I need escape(JSON.stringify(response))
Then I get a full json encoded string with spanish accents.
As a test, I place it in a text file I load with file_get_contents and apply php json_decode and get nothing.
You first need utf8_encode.
And then you get awaiting object of your desire.
After a full day of test and google without any result when decoding unicode properly, I found your post.
So many thanks to you.
Someone asked me to solve the problem of Arabic texts from the Facebook JSON archive, maybe this code helps someone who searches for reading Arabic texts from Facebook (or instagram) JSON:
$str = '\u00d8\u00ae\u00d9\u0084\u00d8\u00b5';
function decode_encoded_utf8($string){
return preg_replace_callback('#\\\\u([0-9a-f]{4})#ism', function($matches) { return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE"); }, $string);
}
echo iconv("UTF-8", "ISO-8859-1//TRANSLIT", decode_encoded_utf8($str));
Facebook Graph API returns JSON objects. Use json_decode() to read them into PHP and you do not have to worry about handling string literal escapes like \uNNNN. Don't try to decode JSON/JavaScript string literals by yourself, or extract chosen properties using regex.
Having read the string value, you'll have a UTF-8-encoded string. If your target HTML is also UTF-8-encoded, you don't need to replace — (U+2014) with any entity reference. Just use htmlspecialchars() on the string when outputting it, so that any < or & characters in the string are properly encoded.
If you do for some reason need to produce ASCII-safe HTML, use htmlentities() with the charset arg set to 'utf-8'.

Passing utf-8 strings between php and javascript

I'm having problems passing utf-8 strings to javascript (ajax). Currently i'm using rawurlencode on the PHP side and unescape on the javascript side.
The problem is in latin and rawurlencode doesn't support it fully.
Is there any alternative or any better option?
The solution was in json_encode functions. The problems stopped when i added JSON_HEX_APOS|JSON_HEX_QUOT.
Thanks!
use json_encode in PHP and receive responses as JSON (jQuery is helpful)
ajax is sent in utf-8 by default, so You just have to return utf-8
php's utf8_encode(data) gets an ISO-8859-1 string as the data argument.
need more suggestions? Tell me where You get the text from ;)
From experience, Javascript's escape() (ant thus unescape()) are not Unicode (UTF-8) friendly. Use encodeURIComponent() and decodeURIComponent() instead.
Anyway, as the docs says:
The escape() function should not be
used to encode URIs.
If php is doing the encoding and js decoding whay not simply not encode in php and encode in js as well? Not really an answer so much as a work around i guess.

replace eurosign in json

Can anyone help me with this one
I have this query and only after adding the last one wich is indexed against the euro
I get invalid json.
$url = 'http://www.google.com/finance/info?client=ig&q=goog,yhoo,AMS:TOM2';
$response= json_decode($response,true);
The only thing different if I directly echo the output is the questionmark in the json.
What would I use to replace the eurosign in the json return?, - and hopefully that will solve it.
thanks in adv, Richard
The JSON is valid ISO-8859-1, or Latin1. If your application is using some other encoding, say UTF-8, you need to convert the encoding of the response from Latin1 to UTF-8.
json_encode and json_decode expect in/output to be utf-8. PHP defaults to use iso-8859-1 as charset. So you may have to convert. (Note that the euro sign doesn't exist in iso-8859-1).

Categories