2
I need to save a file .txt
in UTF-8 in PHP. Saving the file is very simple, however, when opening the file directly in the browser (example www.site.com/file.txt) the browser cannot recognize the UTF-8 encoding and the "Bugam" characters and end up like this:
Having searched the internet, I found some solutions, where people recommend adding GOOD in the file. To add the GOOD is very simple, just use one of these 3 codes.
fwrite($file, "\xEF\xBB\xBF");
fwrite($file, pack("CCC",0xef,0xbb,0xbf));
fwrite($file, chr(0xEF).chr(0xBB).chr(0xBF));
After I added BOM the browser correctly interpreted all the characters and this solved the problem. The end result was more or less like this:
But over time users have reported a problem. The contents of this file can be displayed in two ways on my system. The first way is direct viewing by the equal browser displayed above and the second way is normal viewing on an HTML page. Inside this HTML page there is a <textarea>
with the file text and when using shortcuts CTRL + A
to select everything and CTRL + C
to copy the entire contents of the file displayed in <textarea>
, the control character of the GOOD is copied together, and at the time of pasting the copied content the result is more or less like this:
As you can see, the first character of the copied text is a '?' which in this case is a control character, which is the equivalent of the GOOD.
My doubt is:
- Is there any way to force the browser to read the file in UTF-8 without the need to add BOM to the file?
OR
- Is there any way to read and display the contents of the file. txt in PHP without the GOOD being shown together?
fwrite($file, utf8_encode("This doesn’t solve? Hugs!"));
– jcbrtl
@jcbrtl unfortunately does not solve.... This was the first method I had tested and unfortunately does not solve the problem.
– Eduardo Mior
tried to use mb_convert_encoding?
mb_convert_encoding($content, 'UTF-8')
– Adir Kuhn
I believe that when you make the copy, it copies the allocated garbage in memory, you get to copy some space before the copied text?
– William Silva
@Williamsilva It is impossible to be memory junk because it happens to ALL users, and when I take the GOOD, the character does not come together as I explained above.
– Eduardo Mior
Eduardo, the character of
?
is displayed in place of which character in the generated file?– RXSD
@Andréfilipe this character is not displayed in the file because it is a control character. If you open the file in Vscode the character does not appear, however, if you print the contents of the file in an HTML page and copy all the text and then paste this text into Vscode the character appears. It’s kind of complicated to explain but some users are complaining about it because they like to copy everything straight from the site and paste in their text editors but this character is coming along.
– Eduardo Mior
You scored
<meta charset="utf-8">
in the<head>
of the HTML page where the text of the UTF-8 encoded file is displayed, né?– jcbrtl
@jcbrtl I believe that this has absolutely nothing to do with the problem, but answering your question, yes I marked.
– Eduardo Mior
@Adirkuhn tried to use mb_convert_encoding, but to no avail. In the main tab it looks good, but in the raw text it loses the UTF-8 encoding. Main guide: http://prntscr.com/o8575c. Raw text: http://prntscr.com/o857fc
– Eduardo Mior
what is the main guide and raw text? kkkkk
– Adir Kuhn
@Adirkuhn Main Guide would be my website (I expressed myself in the wrong way). Raw text would be the file itself, in which case the contents of the file opened directly in the browser, for example www.site.com/.txt so you would open the file directly, ie the raw text of the file.
– Eduardo Mior
Unfortunately at no time did you say you were wanting to view a static file
.txt
directly in the browser, I don’t think that until your last edit (8 minutes ago) someone would be able to deduce your problem and solve it. Just for the record in your reply you wrote.htacess
, but it has 2 "c", so.htaccess
. I hope you understand how a constructive criticism, this part of displaying a static doc directly was fundamental to understand, so always describe exactly what you did.– Guilherme Nascimento
@Guilhermenascimento yes I understand, I created the topic in the wrong way. I created the topic 9 days ago and since then I’ve been studying various things and learned a lot about it. The day I created the topic I thought my problem was time to save the file, but only after a while did I discover that actually the problem was time to read the file. I think I owe you an apology.
– Eduardo Mior
@Guilhermenascimento I thought it was impossible to modify the way the browser interprets the static file, so I didn’t even comment on that question, i thought the only way was to modify the way to save the kkkkkkk file but as I said it was a mistake of mine for lack of knowledge.
– Eduardo Mior
ps: the downvotes are not mine (nor in any of the answers), I just commented to guide you on future questions, regardless of how much you understand about something you always have to explain the step by step (still yes simply) and explain exactly where it was displayed that it failed. Good luck my dear.
– Guilherme Nascimento