Weird characters like â are showing up on my site (2024)

Getting weird characters like  instead of or ’? Most likely there is a Character set problem. It can occur when a MySQL and PHP are upgraded or when data has been incorrectly stored or the application is sending an incorrect (or missing) character set to the browser. PHP doesn’t yet support UTF-8 natively in its numerous string handling functions (version 6 will when released).

The Short of it…

1. Don’t use addslashes()! Use mysql_real_escape_string(). Be careful using strlen(), it may count the bytes and no the characters.

2. Send a utf-8 header from php before you send any of the page’s content: header("Content-type: text/html; charset=utf-8");

3. As soon as you connect to mysql, do a mysql_query("SET NAMES 'utf8'"); to set the connection’s encoding to utf-8, which is often necessary in php/mysql apps.

4. You want this meta tag in the <head> section to be absolutely safe:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

5. Good luck!

The long of it…Why is it happening?

First, it’s handy to know a bit about UTF-8. Skip this if you’re already familiar.

UTF-8 uses one or more 8-bit bytes to store a single character, unlike ASCII and friends which use only one byte per character. It is more space-efficient than its cousins (UTF-16, UTF-32) when the majority of the characters can be encoded as a single byte, as is the case with most English text, but with the added benefit that you can still store any character under the sun should you need to. It uses the most significant bits of each byte as continuation bits (to signify that the following byte(s) form part of the same character). It is for this reason that improperly-displayed UTF-8 results in weird characters.

UTF-8 is backwards-compatible with ASCII — all characters up to 127 are identical in both encodings. This at least makes English text legible if the UTF-8 is interpreted incorrectly as ASCII or ISO 8859 character sets. However, it’s these incorrect interpretations that cause the odd characters to appear.

Unfortunately, PHP doesn’t yet support UTF-8 natively in its numerous string handling functions (version 6 will when released), but that doesn’t mean you can’t work with it — you just have to be a bit careful. Let’s take strlen() for example: with plain ASCII text, strlen() returns the number of characters in a string. It does this by counting the number of bytes used to hold the data. It doesn’t know about (and cannot detect) UTF-8 and will blindly count the number of bytes, not the actual number of characters. Hence, the presence of any multibyte characters in your string will give you an incorrect length.

A problem you will inevitably face is when a user takes advantage of another application to create some text which gets pasted into your HTML form and submitted. Microsoft Word, for example, uses Unicode internally and converts characters like quotes and dashes into “smart quotes” and em- and en-dashes automatically. These are typographically correct, but the symbols lie outside the ASCII character set so when copied and pasted, the text is sent as UTF-8 and you end up with multibyte characters all over the place. If you store this text and later send it back to a browser without informing it that you are sending UTF-8, extra characters will appear.

Want more help?

Weird characters like â are showing up on my site (2024)
Top Articles
Unapologetic, Dunleavy stands behind budget vetoes and $3,000 dividend
Important Pros and Cons of Dividend Investing you must know!
Pau.blaz
Administrative Supplement Program to Add Fluid-based Biomarkers and APOE Genotyping to NINDS ADRD Human Subjects Research Grants
Villarica Pawnshop Forex Rate
Her Triplet Alphas Chapter 32
Craigslist Free En Dallas Tx
Sphynx Cats For Adoption In Ohio
Email Hosting » Affordable Mail Solution with Personal Domain | IONOS
'A Cure for Wellness', Explained
organization | QAssurance
Schüleraustausch Neuseeland - Schulabschluss mit Study Nelson
Cooktopcove Com
James Cameron And Getting Trapped Inside Your Most Successful Creation
I've spent £23,000 to stay in the UK but it could all be for nothing
Standard Bank Learnership Programme 2021
Mogadore Reservoir Boat Rental Price
Shore Lodge Webcam
Ticket To Paradise Showtimes Near Movie Tavern Bedford
Cal Poly San Luis Obispo Catalog
14 Must-Know 9GAG Statistics: How Is It Doing in 2023?
Aleksandr: Name Meaning, Origin, History, And Popularity
What is a Nutmeg in Soccer? (Explained!) - Soccer Knowledge Hub
Lee Lucas Jaliyah Dad
Junior&#039;s Barber Shop &amp; Co &#8212; Jupiter
Black Adam Showtimes Near Linden Boulevard Multiplex Cinemas
Circuit Court Peoria Il
Louisiana Physical Therapy Jurisprudence Exam Answers
Tackytwinzzbkup
Hyvee.com Login
Craigslist Mexico Cancun
Roblox Roguelike
Refinery29 Horoscopes
Verizon Fios Internet Review: Plans, Prices And Speed 2024
Franco Loja Net Worth
5Gomovies
2010 Ford F-350 Super Duty XLT for sale - Wadena, MN - craigslist
Middletown Pa Craigslist
O'reilly's In Mathis Texas
Feetfinder Reviews Trustpilot
Plusword 358
Craigslist Lasalle County Il
Carter Williamson Jay Ok
Brokaw 24 Hour Fitness
Footfetish Telegram
Before Trump, neo-Nazis pushed false claims about Haitians as part of hate campaign
Kohl's Hixson Tennessee
How To Spend a Day in Port Angeles (15 Things to Do!)
Stpeach Telegram
Sutter Health Candidate Login
Watch It Horror Thriller movies | Crystal panel
Sdn Michigan State Osteopathic 2023
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 5936

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.