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().
Related
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.
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.
I have a VB6 application that is creating a JSON string and posting it to a website (PHP5). It may look like this:
data=thisisthejsonstringitcontainsthe£hmtlcharacter&code=123&api_key=321
This is an issue because the £ is thought to be the start of a new variable so the json string is being cut.
Does this need to be encoded somehow at the VB source? Or can I do something with this when it arrives at the website? If it needs encoded by VB can anyone suggest a suitable function?
I'm using the application/x-www-form-urlencoded content type when posting.
This might seem far fetched, but have you tried sending £ urlencoded beforehand? being %26pound
If you send url arguments to a webserver, you'll need to urlencode them. That's true for all arguments in formats that don't escape and can contain problematic characters themselves, which includes JSON.
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'.
I'm working on a project in PHP (5.3.1) where I need to send a JSON string to a webservice (in python), but the result I get from json_encode does not pass as a valid JSON (i'm using JSLint to check validity).
I should add that the structure I'm trying to encode is fairly big (13K encoded), and consists partially of UTF8 data, and while json_encode does handle it, i get spaces in weird places in the result. For example, I could get {"hello":tru e} or {"hell o":true} which results in an error from the webservice since the JSON is invalid (or data, like in the second example).
I've also tried to use Zend framework for JSON encoding, but that didn't make much different.
Is there a known issue with JSON in PHP? Did anyone encounter that behavior and found a solution?
You state that "the structure I'm trying to encode ... consists partially of UTF8 data." This implies that it is also partially of non-UTF8 data. The json_encode doc has a comment at the bottom, that
json_encode() expects strings to be encoded to be in UTF8 format, while by default PHP strings are ISO-8859-1 encoded.
This means that
json_encode(array('àü'));
will produce a json representation of an empty string, while
json_encode(array(utf8_encode('àü')));
will work.
Are the failing segments of the JSON due to non-UTF8 input?
For sure object keys cannot contain spaces or any non unicode characters, unquoted variables can be only boolean, integer ,float, object and array value, strings should always be quoted.
Also, I would recommend you to add correct header before your json output.
if(!headers_sent())
header('Content-Type: application/json; charset=utf-8', true,200);
Can you also post your array or object that you passing to json_encode?
I was handling some automatically generated emails the other day and noticed the same weird behavior (spaces were inserted to the email body), so I started to check the email post and found the culprit:
From the SMTP RFC2821:
The maximum total length of a text
line including the is 1000
characters (not counting the leading
dot duplicated for transparency).
My email body was indeed in one line, so breaking it with \n's fixed the spaces issue.
After scratching my head for nearly a day, I've come to the conclusion that the problem was not in the json_encode function. It was with my post function.
Basically, the json_encode was preparing the data to be sent to another service. Before today, I've used stream_context_create and fopen to post data to the external service, but now I use fsockopen and fputs and it seems to be working.
Although I'm unsure as to the nature of the problem, I'm happy it works now :)
BTW: After this process, I mail myself the input and output (both in JSON) and this is how I saw there was a problem in the first place. This problem still persists but I guess that's related to the encoding of the mail or something of that sort.