utf8 without BOM encoding in eclipse
Asked Answered
R

2

12

After some headache I figured out that eclipse using set encoding UTF8 (with BOM) causes an error. It causes whitespace to be added when you use an include causing the headers of a webpage to render within the body in chrome.

ie. on index.php with no gap before or after the of course

<?php include_once('header.php'); ?><body>test</body>

and header.php having (without gaps again of course)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test title</title>
</head>

Then the test title appears within the body (not in view source, but in the console in chrome). This causes a gap at the top of the page.

Opening the index.php and header.php in notepad++ and changing the encoding to UTF8 without BOM solves the problem. How can I fix this in Eclipse?! Switching to notepad++ is not desireable, too many good features in eclipse that are useful (better autocomplete, automatic versioning etc).

A mystery to me...

Romanism answered 11/5, 2012 at 10:12 Comment(4)
File -> Properties -> Resource -> Text Encoding. However I don't see tge BOM option ....Sheetfed
thats the problem, i can only change to UTF8 with BOM which doesn't help..Romanism
Take a look hereSheetfed
:( so im stuck with converting each file manually with notepad++ then...Romanism
U
9

In eclipse, just change the encoding of the file to iso (Right click on file - > Properties) and delete the first three BOM characters, save file and reopen it.

Utrillo answered 28/7, 2015 at 7:56 Comment(1)
I found it easier to use Edit->Set Encoding. Set it to ISO, delete the characters, save, set it back to UTF-8. No need to close and reopen the file.Timberlake
L
7

As far as I'm aware, as long as your workspace (or file-type) settings are set to UTF-8, new BOMs are not created for new files, but old BOMs (created in other editors) will be maintained or even displayed depending on what editor is opening the file (PHP editor, vs PHP+HTML editor).

If you're having random BOMs in your file, it's probably because someone on your team is using a different editor with BOMs on by default (hello, Dreamweaver!). Whenever this person creates a new file, a BOM will be inserted and maintained both by your and his editor (but if one of you removes it it's going to be fine).

If you're bash-savvy, I'd suggest using the following script to prune BOMs in bulk:

sed '1 s/\xEF\xBB\xBF//' < input > output


BOM Magic source: http://thegreyblog.blogspot.com/2010/09/shell-script-to-find-and-remove-bom.html

Loleta answered 4/3, 2013 at 11:7 Comment(1)
Good solution, the bash published in the site is very useful as it can process many files in one row.Shabuoth

© 2022 - 2024 — McMap. All rights reserved.