How to decode utf8 data into simple string in php - javascript

I am facing issue while decoding a utf8 string into simple string which is i am getting from android. String contains both words and emoji's.
For Example:
I am storing this type of data in mysql db \u263A\uD83D\uDE22\uD83D\uDC4D\uD83D\uDE0A\uD83D\uDE0A\uD83D\uDC90. But fail to decode in readable format.

If you treat it as JSON object, it will work:
<?php
$input = '{"key":"\u263A\uD83D\uDE22\uD83D\uDC4D\uD83D\uDE0A\uD83D\uDE0A\uD83D\uDC90"}';
$output = json_decode($input, true);
print $output["key"];
Output:
β˜ΊπŸ˜’πŸ‘πŸ˜ŠπŸ˜ŠπŸ’

In javascript you can use decodeURIComponent for that, like this:
function decode(s) {
return decodeURIComponent(escape(s));
}

Related

Decode JavaScript btoa() encoded string using Java/Kotlin Base64 decoder

I have an encoded string as follows:
S0VEQUkgUlVOQ0lUIFZSSU5FIEVOVEVSUFJJU0UgLSBLRyBTSU1QQU5HIDQgU09PSw
This was encoded using JavaScript btoa() function. This string can be correctly decoded using JavaScript atob() function. It should give the following decoded string :
KEDAI RUNCIT VRINE ENTERPRISE - KG SIMPANG 4 SOOK
Right now I'm developing Android app and I have to decode this string, I'm using the following to decode :
java.util.Base64.getDecoder().decode(encodedstring).toString();
I'm not getting the correct decoded output. Anyone knows what's the problem ? Is it even possible to decode using Java ?
decode(String) returns a byte[], you need to convert that to a string using a String constructor and not the toString() method:
byte[] bytes = java.util.Base64.getDecoder().decode(encodedstring);
String s = new String(bytes, java.nio.charset.StandardCharsets.UTF_8);
It looks like you need mime decoder message
java.util.Base64.Decoder decoder = java.util.Base64.getMimeDecoder();
// Decoding MIME encoded message
String dStr = new String(decoder.decode(encodedstring));
System.out.println("Decoded message: "+dStr);

Decode in JavaScript encoded Base64 value in PHP

I'm trying to decrypt "window.btoa" in PHP, please see my PHP code in below.
<script>
var url = "?url=";
var input = 'some text';
var encrypt = window.btoa( input );
var link = "www.domain.com/"+url+encrypt;
</script>
My Link generated as below
www.domain.com/?url=c29tZSB0ZXh0
PHP code in below
<?php
$testURL = $_GET['url'];
echo $testURL;
?>
Please guide me how to decrypt this value.
The Javascript btoa function will encode your string to base-64. To decode the result in PHP use the base64_decode function;
<?php
echo base64_decode('c29tZSB0ZXh0');
?>
Will print;
some text
btoa is not encryption, it is encoding, it is Base64 encoding badly named: Base64 uses the "A-Z", "a-z", "0-9", "+", "/" and "=" characters to encode the string.
See Window btoa() Method, first hit on Google for "window.btoa".
Using Base64 -> hexadecimal string decoder c29tZSB0ZXh0 is decoded to hex: 736F6D652074657874 and text: some text.

How to convert Encoded url into JSON format?

Here I want to convert my encoded url into JSON format. The encoded url is:
http://localhost:63342/AngularJs/services/e_cell.html#!/%7B%22book%22:%22ABC%22%7D
As much as I understand from your URL you are trying to post this %7B%22book%22:%22ABC%22%7D data in query string.
So first you need to decode your URL encoded data into an string which can be parsed. For that you can take help of decodeURIComponent() javascript API.
decodeURIComponent() - this function decodes an encoded URI component back to the plain text i.e. like in your encoded text it will convert %7B into opening brace {. So once we apply this API you get -
//output : Object { book: "ABC" }
This is a valid JSON string now you can simply parse. So what all you need to do is -
var formData = "%7B%22book%22:%22ABC%22%7D";
var decodedData = decodeURIComponent(formData);
var jsonObject = JSON.parse(decodedData);
console.log(jsonObject );
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string
The decodeURIComponent function will convert URL encoded characters back to plain text.
var myJSON = decodeURIComponent("%7B%22book%22:%22ABC%22%7D");
var myObject = JSON.parse(myJSON);

Why am I getting an error message about "unexpected non-whitespace after JSON data"

Can anybody tell me what the problem is with this:
I am echoing out a PHP array into javascript as follows:
<?php
$myArray=array();
foreach ($persons as $person) {
array_push($myArray,$person['id']);
}
?>
$(document).ready(function() {
populatePersons(JSON.parse(<?php echo json_encode($myArray);?>));
});
So basically I am echo'ing out a PHP array in json format, and then parsing it in javascript but I am getting this error in my console log:
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
Can anybody tell me what I am doing wrong?
Can anybody tell me what I am doing wrong?
While json_encode produces JSON, you are echoing it into JavaScript. As such it will be interpreted as a JavaScript array literal, not a string containing JSON. Hence you cannot use JSON.parse.
Just get rid of it:
populatePersons(<?php echo json_encode($myArray);?>);
If you looked at the generated code, you would something like:
populatePersons(JSON.parse([1,2,3]));
But JSON.parse expects a string (containing JSON). Because JavaScript performs type conversion, it will convert the array to a string first, which likely does not result in valid JSON.
Again: You already have an array, there is no need to parse anything.
Its because youre feeding JSON.parse an array. Just get rid of the JSON.prase in your javascript and replace it with JSON.stringify if youre trying to display the json. If not, then json_encode($myArray) should be enough for operations.
<div id = 'test'></div>
<script>
var test = document.getElementById('test');
test.innerHTML = JSON.stringify(<?php echo json_encode($myArray)?>);
</script>
Try putting the json_encode string in quotes.
populatePersons(JSON.parse('<?php echo json_encode($myArray);?>'));
As the parameter expected is string.

Decode string to valid JSON in javascript or jQuery

I am rendering string javascript variable init statements at server side (using ASP MVC, but I do not think it matters) The string variable content is a valid JSON at server side, and encoded when rendered to html. My rendered statement looks like this:
var myvariable = '{"prefix":"","name":"Grid&...
Quatation marks and other special chars in the string were encoded to html entities, which is completely OK, unless this encoding the variable declaration would by syntactically incorrect.
However I must get back the original string content at client side which was a correct JSON at server side. How can I accomplish this with javascript or jQuery? (Please note, then I do know how to get a javascript object from JSON, I am not asking for that)
How about?
var myjsonobject = JSON.parse(decodeHtml(myvariable));
function decodeHtml(html) {
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
Note that it's not tested
EDIT: Tested it
https://jsfiddle.net/Lmz20s5z/
EDIT2: *See the console log for results
On server side you can escape quotation marks with a backspace \" then you can just do JSON.parse(string) on that string clientside.
JSON.parse('{\"prefix\":\"\",\"name\":\"Grid\"}');

Categories

Resources