Character encodings

A character encoding determines how each letter is stored as bytes in a file, in a database, and in the server's response. This cheat sheet brings together the common encodings for Hebrew text, the causes of gibberish, and the ways to set UTF-8 on your website, in your database, and in your files. You can search, filter by topic, and copy with a click.

Common encodings

  • UTF-8

    A Unicode encoding in which each character takes one to four bytes. ASCII characters take a single byte, exactly as in ASCII, and a Hebrew letter takes two bytes. The HTML standard requires this encoding for web pages, and it's also the right choice for files and databases.

    Example: א (alef) = U+05D0 = D7 90

  • EF BB BF

    The UTF-8 BOM (byte order mark): three bytes at the start of a file that mark it as UTF-8. The BOM is optional. A browser that finds it prefers it over any other declaration, but in files that a program expects to start with specific characters, such as a script that starts with #!, it can break the parsing.

    Example: Check: head -c 3 file.txt | od -An -tx1

  • UTF-16

    A Unicode encoding in 16-bit units. Most characters, including Hebrew, take two bytes, and rare characters such as emoji take a pair of units, that is, four bytes. A BOM at the start of the file marks the byte order. It isn't suitable for web pages, which must be in UTF-8.

    Example: FF FE = UTF-16LE | FE FF = UTF-16BE

  • UTF-32

    Every character is stored in exactly four bytes, so it's easy to calculate the position of any character, but Hebrew text takes twice as much space as in UTF-8.

    Example: א (alef) = 00 00 05 D0 (UTF-32BE)

  • windows-1255

    A legacy Windows encoding for Hebrew, one byte per character, which includes the letters, the vowel points (niqqud), and the shekel sign. Common on older websites and in text and CSV files created by older programs. Browsers also recognize the names cp1255 and x-cp1255 as the same encoding.

    Example: א (alef) = E0 | ₪ = A4

  • ISO-8859-8

    A legacy Hebrew encoding with one byte per character, with letters but no vowel points. The WHATWG Encoding Standard defines it separately from ISO-8859-8-I because it affects the layout direction: this is visual Hebrew, where the text is stored in display order.

    Example: Other names: iso-8859-8, hebrew, visual

  • ISO-8859-8-I

    The same letters as ISO-8859-8, in logical order: the text is stored in reading order, and the browser sets the display direction.

    Example: Other names: iso-8859-8-i, logical

  • iso-8859-1

    In browsers, the names iso-8859-1, latin1, and ascii are decoded as windows-1252, a Western European encoding without Hebrew letters. A Hebrew page declared with one of them shows up as gibberish.

    Example: Fix: Content-Type: text/html; charset=utf-8

Gibberish: symptoms and causes

  • שלו×

    The Hebrew word shalom, saved in UTF-8 and decoded as windows-1252. The × character repeating before every letter is a clear sign of UTF-8 Hebrew read in a Western encoding: the page declares the wrong encoding, or the text was encoded twice on its way to the database.

    Example: Fix: Content-Type: text/html; charset=utf-8

  • ����

    The replacement character U+FFFD in place of every letter: a file in a legacy encoding, such as windows-1255, read as UTF-8. The bytes of the letters aren't a valid UTF-8 sequence, so each letter is replaced by this symbol.

    Example: Fix: iconv -f WINDOWS-1255 -t UTF-8 page.html -o page-utf8.html

  • ????

    Plain question marks in place of Hebrew: the text was converted to an encoding that has no Hebrew letters, for example in a database column or connection set to latin1. The letters are lost in the conversion and can't be recovered from the stored data.

    Example: Fix: SET NAMES 'utf8mb4';

  • 

    Three odd characters at the top of the page or in the first cell of a table: a UTF-8 BOM read in a Western encoding. The text itself is fine, and the program that read it didn't recognize the BOM.

    Example: Remove: sed -i.bak '1s/^\xEF\xBB\xBF//' file.txt

Declaring the encoding on the website and server

  • <meta charset="utf-8">

    Declares the encoding inside an HTML page. Under the HTML standard, the value must be utf-8, and the whole tag must appear within the first 1024 bytes of the page, so it's common to put it first inside head. Only one such tag is allowed per page.

  • Content-Type: text/html; charset=utf-8

    The header the server sends with the page. The browser prefers it over a meta tag inside the page, and only a BOM at the start of the file overrides it.

    Example: Check: curl -sI https://example.com | grep -i content-type

  • AddDefaultCharset UTF-8

    An Apache directive that adds the encoding to the Content-Type header, only in text/html and text/plain responses. You can set it in an .htaccess file when the server allows it. Use it only when every page on the website is saved in UTF-8.

    Example: Default: AddDefaultCharset Off

  • AddCharset UTF-8 .css .js

    An Apache directive that assigns an encoding to files by extension, for example to stylesheets and scripts. You can also set it in an .htaccess file.

  • charset utf-8;

    An Nginx directive that adds the encoding to the Content-Type header. The default is off. It applies to text/html and to the types listed in charset_types.

    Example: charset_types text/plain text/css application/javascript application/json;

  • default_charset = "UTF-8"

    The php.ini setting that sets the encoding sent by default in the Content-Type header, and the default encoding of functions such as htmlspecialchars. The default value is UTF-8, and leaving it empty isn't recommended.

  • header('Content-Type: text/html; charset=utf-8');

    Sets the header from PHP code, before any output of the page. It overrides default_charset in the response where it's sent.

  • Content-Type: text/plain; charset=UTF-8

    A header in an email message that states the encoding of the message body. Without a charset parameter, the email program assumes US-ASCII, and Hebrew may show up as gibberish. The encoding name is case-insensitive.

MySQL and MariaDB databases

  • CREATE DATABASE shop CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

    Creates a database in utf8mb4, which supports Hebrew and every Unicode character, including emoji. A new table created without an explicit encoding gets the database's encoding.

  • CREATE TABLE posts (id INT PRIMARY KEY, title VARCHAR(255)) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

    Creates a table in utf8mb4. New text columns in the table get its encoding.

  • ALTER TABLE posts CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

    Converts the table and all its text columns to utf8mb4, including the existing data. Back up the table before the conversion. The conversion may change a column's type, for example TEXT to MEDIUMTEXT, and it isn't suitable for a column whose data is stored in an encoding different from the one defined for it.

  • ALTER TABLE posts MODIFY title VARCHAR(255) CHARACTER SET utf8mb4;

    Converts a single column to utf8mb4 while keeping its type.

  • ALTER TABLE posts CHANGE body body BLOB;

    Fixes a column defined as latin1 that actually holds UTF-8 bytes: going through BLOB keeps the bytes as they are, and then you set the correct encoding. Do this only after a backup and after confirming that this is really the case.

    Example: Second step: ALTER TABLE posts CHANGE body body TEXT CHARACTER SET utf8mb4;

  • SET NAMES 'utf8mb4';

    Sets the connection encoding: the encoding in which the client sends statements and receives results. A connection with the wrong encoding is a common cause of gibberish, even when the tables are set correctly.

  • SHOW VARIABLES LIKE 'character_set%';

    Shows the encodings of the server, the current database, and the connection.

  • SHOW CREATE TABLE posts;

    Shows the encoding and collation of the table, as well as those of columns set to a different encoding.

  • utf8mb3

    In MySQL, the name utf8 is an old, deprecated alias for utf8mb3, which stores up to three bytes per character. Hebrew is stored in it, but emoji and other four-byte characters aren't. The MySQL documentation recommends utf8mb4 for every new application.

  • mysql:host=localhost;dbname=shop;charset=utf8mb4

    A PDO connection string (DSN) with the connection encoding, so your code sends and receives Hebrew in UTF-8.

  • $mysqli->set_charset('utf8mb4');

    Sets the connection encoding in mysqli. According to the PHP documentation, this is the preferred way, rather than sending SET NAMES as a query.

Detecting and converting files

  • file -i report.csv

    Shows the file type and the detected encoding. The detection is based on the content, so for a file in a legacy single-byte encoding, it's only a guess.

    Example: Output: report.csv: text/plain; charset=utf-8

  • file --mime-encoding *.txt

    Shows only the encoding, for every text file in the folder.

  • head -c 3 file.txt | od -An -tx1

    Shows the first three bytes of the file, to check whether it has a BOM.

    Example: Output with a BOM: ef bb bf

  • iconv -f WINDOWS-1255 -t UTF-8 old.txt -o new.txt

    Converts a file from windows-1255 to UTF-8, into a new file. Don't redirect the output to the same file (> old.txt), because the file is emptied before the conversion.

  • mkdir -p utf8 && for f in *.txt; do iconv -f WINDOWS-1255 -t UTF-8 "$f" -o "utf8/$f"; done

    Converts every text file in the folder to UTF-8, into a new folder named utf8. The original files stay unchanged.

  • iconv -f UTF-8 -t UTF-8 file.txt -o /dev/null

    Checks that the file is valid UTF-8. If it contains an invalid sequence, iconv stops and reports its position.

  • iconv -l | grep -i 1255

    Looks up the exact name of an encoding in the list of encodings that iconv knows.

  • iconv -c -f UTF-8 -t WINDOWS-1255 in.txt -o out.txt

    Converts from UTF-8 to a legacy encoding, for example for an older system. The -c option drops characters that don't exist in the target encoding, such as emoji, instead of stopping the conversion, so those characters are lost.

  • sed -i.bak '1s/^\xEF\xBB\xBF//' file.txt

    Removes the BOM from the start of the file. The original file is saved as file.txt.bak.

  • printf '\xEF\xBB\xBF' | cat - data.csv > data-bom.csv

    Adds a BOM to a UTF-8 CSV file, so Excel recognizes the encoding and displays the Hebrew text correctly.

Hebrew URLs and domains

  • %D7%90

    The Hebrew letter alef in a URL: the character is converted to its UTF-8 bytes, D7 90, and each byte is written as a % sign followed by two hexadecimal digits. The standard recommends uppercase hex digits, but lowercase is also valid.

    Example: /מידע-טכני = /%D7%9E%D7%99%D7%93%D7%A2-%D7%98%D7%9B%D7%A0%D7%99

  • xn--

    A Hebrew domain name is stored in DNS in Punycode form, which starts with xn--. This isn't a character encoding conversion, but a special method for domain names.

    Example: דוגמה = xn--6dbbec0c

The difference between Unicode and a character encoding

The Unicode standard gives every character a fixed number, called a code point. The Hebrew letter alef, for example, is U+05D0, and the Hebrew letters are in the range U+0590 to U+05FF. The encoding determines how that number is stored as bytes: in UTF-8, alef is stored as D7 90, and in windows-1255 as a single byte, E0. Gibberish appears when bytes saved in one encoding are read in another.

How the browser chooses a page's encoding

The browser first checks whether the file starts with a BOM. If there's no BOM, it uses the encoding in the Content-Type header that the server sends, and only when the header has no encoding does it look for a <meta charset> tag at the top of the page. So the safe approach is to save all your files in UTF-8 and declare UTF-8 both in the header and in the page. You can check the headers your server sends with the HTTP header analyzer tool, and you'll find server configuration examples in the .htaccess cheat sheet and the Nginx cheat sheet.

Opening a Hebrew CSV file in Excel

A UTF-8 CSV file saved with a BOM opens normally in Excel. A file without a BOM may open as gibberish. In that case, add a BOM with the command in the Detecting and converting files section, or import the file from the Data > Get Data > From File > From Text/CSV menu.

Hebrew in URLs and domain names

An address with Hebrew words is sent over the network as percent-encoded UTF-8 bytes, and the browser displays it in Hebrew again. You can convert an address in both directions with the URL encoder and decoder tool, and convert a Hebrew domain name to Punycode with the IDN converter (Punycode) tool.

Did you know?

With our web hosting plans, you manage your website's files and databases from the control panel, so when gibberish shows up, it's easy to check the file or table and fix the encoding.

Web hosting plans

Character encodings FAQ

Why does my website show gibberish instead of Hebrew?

Gibberish appears when text saved in one encoding is read in another. Characters such as ש point to UTF-8 text read in a Western encoding, and � symbols point to a file in a legacy encoding, such as windows-1255, read as UTF-8. To fix it, check that your files are saved in UTF-8, that the server sends charset=utf-8 in the Content-Type header, and that the database connection is set to utf8mb4.

Which encoding is best for a Hebrew website?

The right encoding is UTF-8. The HTML standard requires it for web pages, and it supports Hebrew, vowel points, emoji, and every other language on the same page. In a MySQL or MariaDB database, choose utf8mb4, which supports all of these characters.

How do I open a Hebrew CSV file in Excel without gibberish?

Save the file in UTF-8 with a BOM, so Excel recognizes the encoding and displays the Hebrew text correctly. For an existing file without a BOM, you can add one with the command printf '\xEF\xBB\xBF' | cat - data.csv > data-bom.csv, or import it in Excel from Data > Get Data > From File > From Text/CSV.

What's the difference between utf8 and utf8mb4 in MySQL?

In MySQL, the name utf8 is an old alias for utf8mb3, which stores up to three bytes per character. Hebrew is stored in it normally, but emoji and other four-byte characters aren't. The utf8mb4 encoding supports every character, and the MySQL documentation recommends it for every new application.

How do I convert a file from windows-1255 to UTF-8?

On the Linux command line, run iconv -f WINDOWS-1255 -t UTF-8 old.txt -o new.txt, which creates a new UTF-8 file and leaves the original unchanged. Then check the result with file -i new.txt. Most text editors also let you open a windows-1255 file and save it again as UTF-8.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement