English 中文(简体)
PHP - UTF8 problem with German characters
原标题:

I m at my wits end with this one, then I remember stack overflow.

I have a site, http://bridgeserver3.co.uk/disklavier/de/ , the language is stored in a simple PHP file which looks like..

$translations["welcome_text_3"]="Zum Lieferumfang eines jeden Disklaviers gehren bereits zahlreiche Lieder und das Angebot lsst sich jederzeit erweitern. Unsere neuen Modelle warten sowohl mit akustischen als auch mit digitalen Errungenschaften auf, darunter die Wiedergabe von Audio- und MIDI-CDs sowie die revolutionre ãPianoSmartªÓ-Funktion fr die Synchronisation Ihrer Klavieraufzeichnungen mit handelsblichen Audio-CDs. Und wenn Sie schon eine Weile davon trumen, eines Tages zumindest passabel Klavier spielen zu knnen, hilft Ihnen die neue ãSmartKeyªÓ-Technologie beim Einstudieren Ihrer ersten Stcke und versieht Ihr Spiel sogar mit professionellen Verzierungen.";

(The characters show up perfectly in the file, but not on SO).

This text was exported from an excel spreadsheet and the flat file created manually.

The page encoding is UTF8, and I ve added:

header("Content-type: text/html; charset=UTF-8");

to the php file, yet when I echo out the string it lose the German characters.

Can anyone offer any tips?

James

最佳回答

I found the problem...

Excel exported the text with Windows encoding, so it looked correct even in TextMate on my mac. When I reopen with UTF8 encoding the problem was visible in the translations file.

To resolve I used EditPadPro on a PC to convert to UTF8.

Phew.

问题回答

Maybe you could try adding this inside your <head> tag?

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

I use Coda but I think textmate has to offer some option like Text->Encoding->Unicode UTF-8, activate it and then your file will be encoded properly and then save it. Anyway if you are going to put some forms and you are not sure the people is using the correct encoding mode you ll have the same problem.

Use something like this:

<?php

$not_utf8 = "An invalid string"; //Put here the bad text for testing
$not_utf8 = mb_convert_encoding($not_utf8,  UTF-8 , mb_detect_encoding($not_utf8));

echo htmlspecialchars($not_utf8, ENT_QUOTES,  UTF-8 ); //The output should be ok, see the source code generated and search for the correct html entities for these special chars

?>

Hope this help you!

Luis.

The page encoding is UTF8, and I ve added:

Probably not. Make sure that the php file is actually saved with utf-8 encoding.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签