I have to decode a large string base64 encoded:
<?php
$str ='base64code';
echo base64_decode($str);
?>
The link contains the base64 encoded string: http://www.interwebmedia.nl/dataxi/base64.txt
Online decoders give the right result but this php function doesn't. Is there a solution?
base64_decode outputs exactly what was encoded before. It does not adapt any contained values.
You are writing everything out in HTML context. And there any <tags> will not be shown in the browser window. Use show source. Or htmlspecialchars.
Related
I receive json answer from another script. Next I used $json = json_decode($json) and die(json['message']) for show specific string, and this value contains Cyrillic data.
Function mb_detect_encoding() shows that string in UTF-8.
Ok, I use charset="utf-8" in html file, but
I see this output "Пользователь с этим адресом электронной почты уже существует" in my browser.
I used mb_convert_encoding(json['message'], 'UTF-8'), without any effect/
Only var_dump($json) shows me decoded string.
Maybe I wrong to access data in json?
Use mb_convert_encoding(json['message'], "utf-8", "windows-1251"); to properly convert string.
I have some data in raw email format with base64 encoding, but when I put that encoding into a string variable, and then pass it to the echo base64_decode($str) function, it prints out the above.
Here is a snippet of my code. I didn't post the entire base64 encoding string for obvious reasons.
$str = "JVBERi0xLjcKJeTjz9IKNCAwIG9iago8PC9MZW5ndGggMyAwIFIvRmlsdGVy
L0ZsYXRlRGVjb2RlPj4Kc3RyZWFtCnicvVndb9s2ECdgYB28AlnQFGv6ARhY
mg8gVUXxQ1K3vQyO46YGHDt23Dbe07YOGJqH5nF//X53pGXJllzZ8QbZEilK
d7873h2PJ9kKcbySOMVp1Pr9thkGkUILFx1LvijZuvur+aUp+VnZ0iH98Oiv";
echo base64_decode($str);
Here's the string:
%d0%91%d0%b5%d0%b7
I think it's cyrillic and I need it converted to something readable.
mb_detect_encoding() states it's ASCII.
When I do iconv('ASCII', 'UTF-8', $str), it shows me the same string.
Judging by this article, looks like it's in UTF-8, but how do I decode that into readable UTF?
Please help
UPDATE the following site was able to decode the text: http://2cyr.com/decode/?lang=en (thanks to Faiz Rasool for pointing out). The preset I used there is source=utf-8; postfilter=urlencoded, but I've no idea how to reproduce that on my server.
This string look like url encoded try this.
<?php
$str = "%d0%91%d0%b5%d0%b7";
echo utf8_encode(urldecode($str));
?>
I have a json array which is holding the correct string independent of language but when the json is encoded and wrriten into the file it doesnot have the correct values. Its has the the other value random english alphabets eg:(uuadb) I want to write a string into a file where the string could be in any language.Now i am testing with tamil language. But i found PHP doesn't support unicode. please help me how to write unicode charaters into the file using PHP.
I tried using pack function but how to use the pack function for any languages Or is there any other way of doing this.Please help me......
My guess is that you're seeing \uXXXX escapes instead of the non-ASCII characters you asked for. json_encode appears to always escape Unicode characters:
<?php
$arr = array("♫");
$json = json_encode($arr);
echo "$json\n";
# Prints ["\u266b"]
$str = '["♫"]';
$array = json_decode($str);
echo "{$array[0]}\n";
# Prints ♫
?>
If this is what you're getting, it's not wrong. You just have to ensure it's being decoded properly on the receiving end.
Another possibility is that the string you're passing is not in UTF-8. According to the documentation for json_encode and json_decode, these functions only work with UTF-8 data. Call mb_detect_encoding on your input string, and make sure it outputs either UTF-8 or ASCII.
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'.