cook
  • Home
     
  • About Me
     

Common mistakes when working with accentuated characters

Jimmy Bourassa | March 2, 2009

Working with accentuated characters in CakePHP can definitely be a pain if you’re unfamiliar with a few basic guidelines.

All your files must be encoded in UTF-8

Make sure all your files containing accentuated characters are encoded in UTF-8 (no BOM or it’ll screw everything up!). If your file does not have the correct encoding, any string that contains an accentuated character will just be considered as empty, which can be frustrating to debug.

Database connection must use UTF-8 encoding

This will prevent elements from the database to be considered as empty.

//In /app/config/database.php
class DATABASE_CONFIG {
	var $default = array(
		'driver' => 'mysql',
		'persistent' => false,
		'host' => 'some_host',
		'login' => 'some_user',
		'password' => 'some_ow',
		'database' => 'some_db',
		'prefix' => '',
		'encoding'=>'utf8'
	);
}

Set App.Encoding to UTF-8 in core.php

Actually, I’m not sure why we’re doing this, but I figured it couldn’t hurt. Everytime I start a project I make sure my App.Encoding is set to UTF-8 in core.php.

Configure::write('App.encoding', 'UTF-8');

Make sure to define the charset in your html files

Definitely not as hard to debug as the other, but remember to define the charset in your layout.

echo $html->charset();

Hope it helps!

Comments rss
Comments rss

« Model “X” with multiple associations leading to Model “Y” OpenX and HostGator shared hosting »

8 responses

Hi Jimmy I've a tip to share. If you make a

Sebastián | March 3, 2009

Hi Jimmy

I’ve a tip to share.

If you make a file UTF-8(no BOM) in your code editor but the file doesn’t contains any UTF-8 character, when the file is saved and later reopened will be detected as an ANSI/ASCII file.

It could lead to unexpected & weird errors to work in a file that you believe is UTF-8 and actually is a ANSI/ASCII file.

To avoid this I have a method:

1. Save your file in UTF-8 (no BOM).
2. Add a comment in the first lines. I generally use something like:

 

3. Save your file again.

Now, when you open the file is always detected as UTF-8.

I hope this could help somebody.

PS: excuse my poor english please.

In step 2 the code must be: #php_open_tag# // Ñandú

Sebastián | March 3, 2009

In step 2 the code must be:

#php_open_tag# // Ñandú #php_close_tag#

Hello Sebastian, Thanks for the tip. I don't think my code

Jimmy Bourassa | March 3, 2009

Hello Sebastian,

Thanks for the tip. I don’t think my code edit (Coda from Panic (OS X)) behave like this - but it might explain the troubles I’ve had with Dreamweaver when I started with Cake.

Out of curiosity, what editor do you use?

Maybe Coda is more intelligent handling utf-8 files (I would

Sebastián | March 3, 2009

Maybe Coda is more intelligent handling utf-8 files (I would like to use it on Windows!) .

I currently use Scriptly for Windows but the tip is valid for Dreamweaver or Notepad++. Scriptly is a lightweight freeware editor with many interesting features. It’s still a little buggy, but editing HTML, CSS & PHP on it is very confortable for me.

Regards,

[...] Bourassa has a series of tips on how to

CakePHP Digest #15 - Blog Posts Edition | PseudoCoder.com | June 1, 2009

[...] Bourassa has a series of tips on how to successfully use accented characters. His post can pretty much be summed up as: use UTF-8 or fail [...]

This has been a super moment for myself. I have

TRX Suspension Trainer Review | August 28, 2011

This has been a super moment for myself. I have had a great time here. Reading this post has been very infomational, and I am really pleased that I stopped by. Super Rad!

Do not enough cash to buy a house? Worry not,

Mckinney35Misty | September 4, 2011

Do not enough cash to buy a house? Worry not, just because this is possible to take the loan to solve such problems. Therefore get a consolidation loan to buy everything you require.

I took 1 st mortgage loans when I was 20

personal loans | November 9, 2011

I took 1 st mortgage loans when I was 20 and it supported my business a lot. But, I require the commercial loan again.

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">

Recent Posts

  • OpenX and HostGator shared hosting
  • Common mistakes when working with accentuated characters
  • Model “X” with multiple associations leading to Model “Y”
  • Sort search results by relevance
  • Redirecting to the proper page after actions

People I like

  • Jean-Philippe Doyle
  • Knowland Design
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox