how to convert Base64 string into html in laravel 9 - html

Base64 string exist in Database, I am not able to convert base64 string into html in laravel 9.
Online base64 to html converter convert my base64 string easily .
here are my base64 string
"PHAgY2xhc3M9Im13LWVtcHR5LWVsdCI+CjwvcD4KPHA+VGhlIDxiPmdyZXkgdGluYW1vdTwvYj4gKDxpPlRpbmFtdXMgdGFvPC9pPikgaXMgYSB0eXBlIG9mIGdyb3VuZCBiaXJkIG5hdGl2ZSB0byBTb3V0aCBBbWVyaWNhLiBGb3VyIHN1YnNwZWNpZXMgYXJlIHJlY29nbmlzZWQuCjwvcD4KCgo8aDI+PHNwYW4gaWQ9IlRheG9ub215Ij5UYXhvbm9teTwvc3Bhbj48L2gyPgo8cD5BbGwgdGluYW1vdSBhcmUgZnJvbSB0aGUgZmFtaWx5IFRpbmFtaWRhZSwgYW5kIGluIHRoZSBsYXJnZXIgc2NoZW1lIGFyZSBhbHNvIHJhdGl0ZXMuIEFsbCByYXRpdGVzIGV2b2x2ZWQgZnJvbSBwcmVoaXN0b3JpYyBmbHlpbmcgYmlyZHMsIGFuZCB0aW5hbW91cyBhcmUgdGhlIGNsb3Nlc3QgbGl2aW5nIHJlbGF0aXZlIG9mIHRoZXNlIGJpcmRzLjwvcD48cD5UaGUgZ3JleSB0aW5hbW91IGhhcyBzZXZlcmFsIHN1YnNwZWNpZXM6CjwvcD4KPHVsPjxsaT48aT5ULiB0LiBsYXJlbnNpczwvaT4gIHdpdGggYSByYW5nZSBpbiB0aGUgbW9udGFuZSBmb3Jlc3RzIG9mIGNlbnRyYWwgQ29sb21iaWEgYW5kIG5vcnRod2VzdGVybiBWZW5lenVlbGEuPC9saT4KPGxpPjxpPlQuIHQuIGtsZWVpPC9pPiB3aXRoIGEgcmFuZ2Ugb2Ygc291dGgtY2VudHJhbCBDb2xvbWJpYSwgZWFzdGVybiBFY3VhZG9yLCBlYXN0ZXJuIFBlcnUsIGVhc3Rlcm4gQm9saXZpYSwgYW5kIHdlc3Rlcm4gQnJhemlsLjwvbGk+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+"
I am trying for this issue
$file = "data:#file/html;base64,".base64_encode(file_get_contents($base64String));

just use base64_decode() to get the html back, as
$generatedHtml = base64_decode($base64String);

Related

Convert Json String in Text file to Json file with Python 3

I have gathered around 5000 tweets using Twitter API but mistakenly wrote them into a .txt. I need them to be in .json format instead. I tried several solutions from simply changing the file format from txt to json to 3 hours worth of stackoverflow answers such as this:
import json
f = open("tweets.txt", "r")
content = f.read()
#print(content)
text_file = open("tweets4.json", "w")
text_file.write(content)
text_file.close()
But even this opened like a text string. I also tried to use json.dump() but since the string is already in Json format, it did double encoding and it was full of "\
In short, it looks like this:
Json string in text format
But should look like this (I re ran the code and saved new tweets to a json):
Json format

How to escape JSON string for CSV parsing?

A bit of an odd question perhaps.
I'm trying to Store a JSON string appropriately in a CSV column as a string. It works OK when generating the CSV file, however parsing the CSV file with the JSON in it is a problem.
I've tried "{"prop": "Val"...}", "{""prop"": ""Val""...}", "{\"prop\": \"Val\"...}", "{\""prop\"": \""Val\""...}"
However none of them parse well at all.
Please help!
The correct answer here is to double quote the JSON string so this:
{ "a": 10 }
converts into this:
"{ ""a"": 10 }"
You can use the following replace logic to escape your JSON string:
`"${YOUR_JSON_STR.replace(/\"/g, '""')}"`

Postgres psql output text wrapping when converting to json

I have a strange behavior in psql that makes long base64 encoded text break with newline when converting to json string
I encode my text in base64 as followed:
db=> select encode('-------------------------------------------------------------------'::bytea, 'base64');
encode
------------------------------------------------------------------------------
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t+
LS0tLS0tLS0tLQ==
Here the output text is wrapped, that's not actually a problem.
But then if I convert this base64 encoded text to a json string using to_json():
db=> select to_json(encode('-------------------------------------------------------------------'::bytea, 'base64')::text);
to_json
--------------------------------------------------------------------------------------------------
"LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t\nLS0tLS0tLS0tLQ=="
Here the base64 encoded text in json has a new line character (\n) near the end which totally breaks the base64 code when decoding (Thanks Laurenz Albe for the correction!). The new line character gives me trouble later in my program and I'm searching a solution to fix it in psql.
I've tried using the /pset format command or setting PAGER="less -SF" psql ... (from other stackoverflow issue: Disable wrapping in Psql output) but without success.
The only solution I've found (and a very dirty one) is to do:
db=> select to_json(regexp_replace((select to_json(encode('----------------------------------------------------------'::bytea, 'base64')::text))::text, '(\\n|")', '', 'g'));
to_json
------------------------------------------------------------------------------------
"LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLQ=="
Here I convert to JSON string (with to_json()) then I remove the JSON string quotes and new line characters (with regexp_replace()) and then re-converting to JSON again to get the expected result.

Decoding string of bytes back to bytes

I need to save a byte string in a json file and get it back as a bytestring.
In order to be able to dump it into the json, I had to convert the bytes to a regular string. The problem I'm having is that once I read the json and try to encode the converted bytestring, the '\' are doubled, so the strings aren't the same.
How could you do it properly? :(
Input:
salt = b'\xd5KS\xe4\x1b\xbd'
Output = b'\xd5KS\xe4\x1b\xbd'

Json parsing with unicode characters

i have a json file with unicode characters, and i'm having trouble to parse it. I've tried in Flash CS5, the JSON library, and i have tried it in http://json.parser.online.fr/ and i always get "unexpected token - eval fails"
I'm sorry, there realy was a problem with the syntax, it came this way from the client.
Can someone please help me? Thanks
Quoth the RFC:
JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.
So a correctly encoded Unicode character should not be a problem. Which leads me to believe that it's not correctly encoded (maybe it uses latin-1 instead of UTF-8). How did you create the file? In a text editor?
There might be an obscure Unicode whitespace character hidden in your string.
This URL contains more detail:
http://timelessrepo.com/json-isnt-a-javascript-subset
In asp.net you would think you would use System.Text.Encoding to convert a string like "Paul\u0027s" back to a string like "Paul's" but i tried for hours and found nothing that worked.
The trouble is hardcoding a string as shown above already decodes the string as you will see if you put a break point on it so in the end i wrote a function that converts the Hex27 to Dec39 so that i ended up with HTML encodeing and then decoded that.
string Padding = "000";
for (int f = 1; f <= 256; f++)
{
string Hex = "\\u" + Padding.Substring(0, 4 - f.ToString().Length) + f;
string Dec = "&#" + Int32.Parse(f.ToString(), NumberStyles.HexNumber) + ";";
HTML = HTML.Replace(Hex, Dec);
}
HTML = System.Web.HttpUtility.HtmlDecode(HTML);
Ugly as sin, I know but without using the latest framework (Not on ISP's server) it was the best I could do and someone must know a better solution.
I had the same problem and I just change the file encoding type Mac-Roman/windows-1252 to UTF-8.. and it worked
I had the same problem with Twitter json files. I was parsing them in Python with json.loads(tweet) but it failed for half of the records.
I changed to Python3 and it works well now.
If you seem to have trouble with the encoding of a JSON file (i.e. escaped codes such as \u00fc aren't displayed correctly regardless of your editor's encoding setting) generated by Python with json.dump s(): it encodes ASCII by default and escapes the unicode characters! See python json unicode - how do I eval using javascript (and python: json.dumps can't handle utf-8? and Why does json.dumps escape non-ascii characters with "\uxxxx").