开发者

javascript, mysql database and escaping 'weird' characters

On my website visitors can do some inline editing. I use ajax for it with a MySQL database and PHP. I expect the Dutch language to be used on the website.

My challenge is to get the character encoding to work w开发者_运维技巧ell.

I could use advice on:

  • the database (do i use utf-8? latin1_swedish_ci)
  • the tables in the database (i'd prefer to heve them similar to the database.)
  • the escaping to use in the ajax call (x = escape(x);)
  • the webpage character set (UTF-8? ISO-something?)
  • how this all works together.

I use nicEdit as javascript wysiwyg editor.

I could of course explain what happens whan I want to save ë and if that helps I will, but I figured it would be best to understand the matter instead of just trying to quick-fix it.

[EDIT] To elaborate:

I use these in my PHP

$input = stripslashes($input); //(if magic quotes are 'on')

$input = mysql_real_escape_string($input);

$input = strip_tags($input, '<strong><em><span><ul><ol><p><a><br><li>');

In my htmlpage:

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

Javascript:

x = excape(x);

Database:

MySQL connection collation: utf8_general_ci

Table options: DEFAULT CHARSET=utf8

This is an example of what happens:

I enter (inline) the word Rëg (using 'option+u' then 'e' on my mac).

I save the word. It shows like this: R�g on the webpage.

In the database i find Rëg.

I open the editor, do nothing but save again and it shows: R%uFFFDg in the database as well as on the page. After that it does not change anymore.

Any help is greatly appreciated.

Kim


It shows like this: R�g on the webpage.

You need to instruct the webbrowser that you're displaying the webpage in UTF-8 and that it should interpret it as the same. Add the following to top of your PHP, before emitting any character to the output:

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

Only the <meta> tag is not enough. This is not used by the webbrowser. It's the response header which counts. By the way, Javascript's escape() function is deprecated.

See also:

  • PHP UTF-8 cheatsheet


Just use UTF-8 for everything, and normally it will just work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜