Encoding of Files for PHP project - php

I am developing a php project which is in HTML5. Following is the meta used for all pages in my website.
<meta charset="utf-8">
I am coding in windows machine using NetBeans. I was not really aware of encoding of the files. Since the code was working fine, i was not giving importance for this.
However, based on some of the questions in stackoverflow, I could understand more about encoding. I noticed that many php/js/css files of my project are saved in UTF-8 encoding whereas some php/js/css files are saved in ANSI encoding. (to understand this, i opened the file in notepad, clicked on save as and checked the default encoding shown).
It seems the files in which I pasted some of the unicode characters were autosaved in UTF-8 and all other files were saved in ANSI encoding (I guess it might be Windows-1252). All this happened even though I set project preference as UTF-8 in netbeans.
Is it required to save those files (files which does not use unicode) also to UTF-8 as my html meta says UTF-8? (Note that there are no issues when I tested my website, but my testing was from a windows machine)
I am also curious to know, how the browser render the web page correctly though some of the php files are saved in ANSI but served with meta UTF-8.

(to understand this, i opened the file in notepad, clicked on save as and checked the default encoding shown).
This isn't an accurate way of checking the encoding of a file.
Files which contain only ASCII characters -- like most CSS and Javascript source files! -- are valid in most text encodings. Notepad will call them "ANSI" because that's its default, but they're also perfectly valid as UTF-8. No conversion is necessary.

Related

Chrome: HTML content inside PHP files not displaying when viewed as a local file

Prior to a recent update to Chrome, I was able to view a almost 95% HTML file locally that was saved as PHP but now with Chrome 67.0.3396.79 it appears this no longer works and instead always shows the source code instead of rendering whatever HTML it finds.
All files begin with the usual DOCTYPE html as expected, so it's the first thing the browser sees.
Previously I had this issue due to saving the PHP file as UTF-8 encoded. Re-saving as ANSI allowed Chrome to display it correctly, however now not even ANSI is working.
I do have a server set up and use it for development and testing in a real environment and the forms work as expected there. Sometimes editing locally is faster and easier for me, but I can use the server always if I must.
Was this an intended change or a new bug?

PHP and UTF-8 (and Windows as well)

Here is the situation:
I'm using UTF-8 to input Japanese characters into a MySQL database, using a php form. However while done from my PC it works perfectly and the script records the characters correctly into the DB, but from other PCs the script inputs raw symbols. I've declared completely all the things regarding the UTF-8 header, meta tag, etc. I'm sure this is not a php/sql issue, (because it works perfectly from one pc) but something from windows configuration I cannot understand.
Anyone knows something regarding this issue?

Setting up cpanel server for PHP UTF-8 characters [BOM error]

I've been having a bit of a headache getting my server working with UTF-8 characters. I have a list of UK towns some of which have Gaelic characters and some Scottish ones also have special characters it seems.
To facilitate this the developer put a BOM at the start of the PHP file. On his server a Ubuntu setup this worked fine as apparently his PHP defaults to UTF-8. However on my server it caused an issue with the headers having been already sent (Obviously reading the BOM as ABC instead of a BOM declaration).
I went into WHM and set the Default_charset in php.ini to UTF-8, and php info reads the default charset as UTF-8. I didn't want to edit the httpd.conf to set Apache to UTF-8 across the entire server as this would possible cause problems elsewhere so I set UTF-8 in the .htaccess file as follows
AddDefaultCharset utf-8
However the BOM was still being read as text and messing up the headers. With the BOM removed the page and the site works fine, just the special characters are read as random ISO characters.
I don;t have much experience with server settings or administration and none with BOM or setting up and running PHP in UTF-8 so any help or leads would be a great help. no doubt I've done something extremely stupid and obvious.

Dreamweaver showing boxes instead of unicode characters

My pages when viewed in browser are working fine. I have a language file (in .php extension), but without any HTML tags. I m using Dreamweaver CS5.5 to edit these files.
I have a variable $lang["label_name"], which holds the value. in dreamweaver code view, all the devnagari unicode character appears as boxes. But in another machine, with same version of dreamweaver, when same file is opened, the unicode characters are showing correctly.
Is there any settings that I missed in my machine? How to make unicode character appear correctly in Dreamweaver (not talking about browser)?
Thanks
Is there any settings that I missed in my machine?
Probably. Just compare the setup between the two dreamweaver installations and add the differences to your question. This might then be easier to say.
How to make unicode character appear correctly in Dreamweaver (not talking about browser)?
By configuring it properly. Also ensure that you are using the same font on both systems and the files have the same encoding and Dreamweaver is aware of the correct encoding.

Migrate web-pages from different char-sets to UTF-8

For the last years I used Notepad++ on Win XP SP2.
As I just have seen, the setting in Notepad++ is to encode new files in "ANSI" in "Windows Format". Basically all files on my harddisk should be ANSI files then, but I'm not sure.
Most .html-files have a charset-tag as "text/html; charset=iso-8859-1", but some have none.
Other files, especially text-files (for example keyword-lists) I stored with Firefox XPCOM-system, I don't know how they are currently encoded.
On Server-side I have Apache with PHP and MySql.
For Upload I used Filezilla.
Now the problem is: I want to use Japanes signs (or arabic, etc.). This only works partly.
I can get my selfmade Firefox-Application to constantly write or read UTF-8. But I can't check everytime which of the old files is which encoding.
Having just read Joel Spolsky's old article about UTF-8 strengthens my view that I simply have to get my whole system changed as much as possible to UTF-8.
As long as I have it running that way locally on my Hard-Disk I could just re-upload everything to the server.
So: How do I get all my files locally transfered to UTF-8?
And: Is it possible at all to have Win XP SP2 using constantly UTF-8 everywhere? Or do I have to check it with every program, or even worse with every file, that the right encoding is to be used.
How about files I get for example in E-Mails or via an USB-stick, or that I download in zip-files? (Or a thousand possibilities more.)
Update:
1.-4. went OK so far. I tried first with BOM, but without seems to be better.
So to 5.) Something I have to change there too. I changed as in 3.) the charset in the html-template-file, and the text coming from the template is displayed correctly. But the text coming from MySql/Php shows the UnknownChar-sign at some places currently, i.e. where there should be Umlaute äöü.
I have changed all collations for text fields in the MySql-Database via phpmyadmin to "utf8_unicode_ci", but that didn't do the trick.
Is it a php-issue, or do I only have to convert somehow the data in the MySql-Database once?
The beauty of UTF-8 is that it's a superset to ASCII, so if your html and php files only contain Latin alphabets (i.e. English and programing/HTML syntax), you don't need to convert the file at all. You can leave most of your file unchanged.
Should you find few exceptions that you want to convert it manually, you may open them up in Notepad++, and do 'Encoding' - 'Convert to UTF-8 (No BOM)'.
Yes, you do need to change/add <meta> charset tag to all the HTML files to make sure the browser render your files in UTF-8.
In Notepad++ you could set the new file to always open with 'UTF-8 (No BOM), Unix'. Also, check the tick on "Apply to ANSI files" so old file can be correctly saved to the new encoding. I suggest the format is because even though you are working on a Windows machine, the web servers usually runs Linux/BSD so the format is the native form (keeping files in native form is important especially when you are using a version control system).
Migrate a live site with database is a different issue. Data in MySQL comes with their own encoding, and from your question I cannot tell if you need to do it and how to do it. Need more specifics on that (if you need to).

Categories