問題描述
我有一個非常奇怪的問題.我有一個包含一些德語字母的網站,當它只有 html 而沒有 php 時,符號是顯示編碼的屬性,當我將其更改為 UTF-8 時,它們不顯示,而不是 ? 我得到 .當我將 html 放入 php 并使用 charset=iso-8859-1 編碼在 Wamp 上使用 Zend studio 啟動它時,我得到 ??? 而不是 ?(我想補充一點,同樣的 ? 是單選按鈕的值).當它在
標簽中時,它會正確顯示.你能告訴我如何解決這個問題.我查看了其他站點,它們使用 UTF-8 編碼并正確顯示相同的符號.我試圖更改 php edior 編碼,但我想這并不重要 -> Zend Studio 的編輯器中的所有內容都正確顯示...提前致謝.
I have this very strange problem. I have a site that contains some German letters and when it's only html without php the symbols are property displayed with encoding when i change it to UTF-8 they dont display and instead of ? I get ?. When I put the html inside php and start it with Zend studio on Wamp with the charset=iso-8859-1 encoding I get ??? instead of ? ( I want to add that this same ? is a value of a radio button). When it's in a
tag it displays properly. Can you tell me how to fix this issue. I look at other sites and they have UTF-8 Encoding and displaying properly the same symbol. I tried to change the php edior encoding but it doesn't matter I suppose -> everything is displaying properly inside Zend Studio's editor... Thank you in advance.
推薦答案
您可能已經開始混合編碼類型.例如.以 iso-8859-1 格式發(fā)送但從 MySQL 或 XML 獲取 UTF-8 文本編碼的頁面通常會失敗.
You have probably come to mix encoding types. For example. A page that is sent as iso-8859-1, but get UTF-8 text encoding from MySQL or XML would typically fail.
要解決此問題,您必須控制與您選擇使用的內部編碼類型相關的輸入編碼類型.
To solve this problem you must keep control on input ecodings type in relation to the type of encoding you have chosen to use internal.
如果您將其作為 iso-8859-1 發(fā)送,則用戶的輸入也是 iso-8859-1.
If you send it as an iso-8859-1, your input from the user is also iso-8859-1.
header("Content-type:text/html; charset: iso-8859-1");
如果 mysql 發(fā)送 latin1,你什么都不用做.
And if mysql sends latin1 you do not have to do anything.
但是,如果您的輸入不是 iso-8859-1,則必須在將其發(fā)送給用戶之前對其進行轉換,或者在存儲之前將其適配到 Mysql.
But if your input is not iso-8859-1 you must converted it, before it's sending to the user or to adapt it to Mysql before it's store.
mb_convert_encoding($text, mb_internal_encoding(), 'UTF-8'); // If it's UTF-8 to internal encoding
簡而言之,您必須始終將輸入轉換為適合內部編碼和轉換器輸出以匹配外部編碼.
Short it means that you must always have input converted to fit internal encoding and convereter output to match the external encoding.
這是我選擇使用的內部編碼.
This is the internal encoding I have chosen to use.
mb_internal_encoding('iso-8859-1'); // Internal encoding
這是我使用的代碼.
mb_language('uni'); // Mail encoding
mb_internal_encoding('iso-8859-1'); // Internal encoding
mb_http_output('pass'); // Skip
function convert_encoding($text, $from_code='', $to_code='')
{
if (empty($from_code))
{
$from_code = mb_detect_encoding($text, 'auto');
if ($from_code == 'ASCII')
{
$from_code = 'iso-8859-1';
}
}
if (empty($to_code))
{
return mb_convert_encoding($text, mb_internal_encoding(), $from_code);
}
return mb_convert_encoding($text, $to_code, $from_code);
}
function encoding_html($text, $code='')
{
if (empty($code))
{
return htmlentities($text, ENT_NOQUOTES, mb_internal_encoding());
}
return mb_convert_encoding(htmlentities($text, ENT_NOQUOTES, $code), mb_internal_encoding(), $code);
}
function decoding_html($text, $code='')
{
if (empty($code))
{
return html_entity_decode($text, ENT_NOQUOTES, mb_internal_encoding());
}
return mb_convert_encoding(html_entity_decode($text, ENT_NOQUOTES, $code), mb_internal_encoding(), $code);
}
這篇關于PHP 弄亂了 HTML 字符集編碼的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!