PHP 日本研討會 2024

mb_convert_encoding

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

mb_convert_encoding將字串從一種字元編碼轉換為另一種

描述

mb_convert_encoding(array|string $string, string $to_encoding, array|string|null $from_encoding = null): array|string|false

stringfrom_encoding(或目前的內部編碼)轉換為 to_encoding。如果 string 是一個 array,其所有 string 值將會以遞迴方式轉換。

參數

string

要轉換的 stringarray

to_encoding

結果的目標編碼。

from_encoding

用於解譯 string 的目前編碼。可以將多個編碼指定為 array 或以逗號分隔的清單,在這種情況下,將使用與 mb_detect_encoding() 相同的演算法來猜測正確的編碼。

如果省略 from_encoding 或為 null,則如果已設定 mbstring.internal_encoding 設定,則會使用該設定;否則會使用 default_charset 設定

請參閱支援的編碼,以取得 to_encodingfrom_encoding 的有效值。

回傳值

成功時回傳已編碼的 stringarray,失敗時回傳 false

錯誤/例外

自 PHP 8.0.0 起,如果 to_encodingfrom_encoding 的值為無效編碼,則會擲回 ValueError。在 PHP 8.0.0 之前,則會發出 E_WARNING

變更記錄

版本 描述
8.2.0 mb_convert_encoding() 將不再回傳以下非文字編碼:"Base64""QPrint""UUencode""HTML entities""7 bit""8 bit"
8.0.0 to_encoding 傳遞無效的編碼時,mb_convert_encoding() 現在會擲回 ValueError
8.0.0 from_encoding 傳遞無效的編碼時,mb_convert_encoding() 現在會擲回 ValueError
8.0.0 from_encoding 現在可為 Null。
7.2.0 此函式現在也接受 array 作為 string。先前只支援 string

範例

範例 #1 mb_convert_encoding() 範例

<?php
/* 將內部字元編碼轉換為 SJIS */
$str = mb_convert_encoding($str, "SJIS");

/* 將 EUC-JP 轉換為 UTF-7 */
$str = mb_convert_encoding($str, "UTF-7", "EUC-JP");

/* 從 JIS、eucjp-win、sjis-win 自動偵測編碼,然後將 str 轉換為 UCS-2LE */
$str = mb_convert_encoding($str, "UCS-2LE", "JIS, eucjp-win, sjis-win");

/* 如果 mbstring.language 為「Japanese」,「auto」會展開為「ASCII,JIS,UTF-8,EUC-JP,SJIS」 */
$str = mb_convert_encoding($str, "EUC-JP", "auto");
?>

另請參閱

新增註解

使用者貢獻的註解 30 則註解

71
josip at cubrad dot com
11 年前
在我最後一個專案中,我需要將多個 CSV 檔案從 Windows-1250 轉換為 UTF-8,在搜尋了幾天後,我找到了一個部分解決我問題的函式,但它仍然沒有轉換所有字元。因此我做了這個

function w1250_to_utf8($text) {
// 基於以下位置的對應
// http://konfiguracja.c0.pl/iso02vscp1250en.html
// http://konfiguracja.c0.pl/webpl/index_en.html#examp
// http://www.htmlentities.com/html/entities/
$map = array(
chr(0x8A) => chr(0xA9),
chr(0x8C) => chr(0xA6),
chr(0x8D) => chr(0xAB),
chr(0x8E) => chr(0xAE),
chr(0x8F) => chr(0xAC),
chr(0x9C) => chr(0xB6),
chr(0x9D) => chr(0xBB),
chr(0xA1) => chr(0xB7),
chr(0xA5) => chr(0xA1),
chr(0xBC) => chr(0xA5),
chr(0x9F) => chr(0xBC),
chr(0xB9) => chr(0xB1),
chr(0x9A) => chr(0xB9),
chr(0xBE) => chr(0xB5),
chr(0x9E) => chr(0xBE),
chr(0x80) => '&euro;',
chr(0x82) => '&sbquo;',
chr(0x84) => '&bdquo;',
chr(0x85) => '&hellip;',
chr(0x86) => '&dagger;',
chr(0x87) => '&Dagger;',
chr(0x89) => '&permil;',
chr(0x8B) => '&lsaquo;',
chr(0x91) => '&lsquo;',
chr(0x92) => '&rsquo;',
chr(0x93) => '&ldquo;',
chr(0x94) => '&rdquo;',
chr(0x95) => '&bull;',
chr(0x96) => '&ndash;',
chr(0x97) => '&mdash;',
chr(0x99) => '&trade;',
chr(0x9B) => '&rsquo;',
chr(0xA6) => '&brvbar;',
chr(0xA9) => '&copy;',
chr(0xAB) => '&laquo;',
chr(0xAE) => '&reg;',
chr(0xB1) => '&plusmn;',
chr(0xB5) => '&micro;',
chr(0xB6) => '&para;',
chr(0xB7) => '&middot;',
chr(0xBB) => '&raquo;',
);
return html_entity_decode(mb_convert_encoding(strtr($text, $map), 'UTF-8', 'ISO-8859-2'), ENT_QUOTES, 'UTF-8');
}
10
Julian Egelstaff
2 年前
如果您的內容看起來像是 ISO-8859-1,但其中包含來自微軟軟體的「智慧引號」,或有人從微軟軟體複製貼上內容,那麼您實際處理的可能是 Windows-1252。試試這個

<?php
$cleanText
= mb_convert_encoding($text, 'UTF-8', 'Windows-1252');
?>

令人困擾的是,自動偵測 (例如:mb_detect_encoding 函式) 通常會認為 Windows-1252 是 ISO-8859-1。很接近,但並不完全是。如果您之後嘗試對產生的文字進行 unserialize,這一點至關重要,因為字串的位元組計數必須完全正確。
40
regrunge at hotmail dot it
14 年前
我一直在嘗試尋找一個在 Mac 上寫的挪威文 (有很多 ø、æ、å) txt 檔案的字元集,我用這種方式找到了它

<?php
$text
= "一個要傳遞的奇怪字串,可能帶有一些 ø、æ、å 字元。";

foreach(
mb_list_encodings() as $chr){
echo
mb_convert_encoding($text, 'UTF-8', $chr)." : ".$chr."<br>";
}
?>

看起來不錯的那一行會告訴您它寫入時的編碼。

希望能幫助到某人
31
volker at machon dot biz
17 年前
大家好。對於正在尋找將 iso 字串轉換為 utf8 或將 utf8 字串轉換為 iso 的函式的人,這是您的解決方案

public function encodeToUtf8($string) {
return mb_convert_encoding($string, "UTF-8", mb_detect_encoding($string, "UTF-8, ISO-8859-1, ISO-8859-15", true));
}

public function encodeToIso($string) {
return mb_convert_encoding($string, "ISO-8859-1", mb_detect_encoding($string, "UTF-8, ISO-8859-1, ISO-8859-15", true));
}

對我來說,這些函式運作良好。試試看
4
Rainer Perske
2 年前
從 PHP 8.2 開始,文字編碼 HTML 實體將被棄用。

為了將所有非 ASCII 字元轉換為實體 (以產生純 7 位元 HTML 輸出),我使用

<?php
echo mb_convert_encoding( htmlspecialchars( $text, ENT_QUOTES, 'UTF-8' ), 'HTML-ENTITIES', 'UTF-8' );
?>

我可以使用以下方式獲得相同的結果

<?php
echo mb_encode_numericentity( htmlentities( $text, ENT_QUOTES, 'UTF-8' ), [0x80, 0x10FFFF, 0, ~0], 'UTF-8' );
?>

輸出包含一些常用字元的著名命名實體,以及其餘字元的數值實體。
11
francois at bonzon point com
16 年前
aaron,為了捨棄不支援的字元而不是列印 ?,您不妨直接設定組態指令

mbstring.substitute_character = "none"

在您的 php.ini 中。務必將 none 用引號括起來。或在執行時使用

<?php
ini_set
('mbstring.substitute_character', "none");
?>
14
aaron at aarongough dot com
16 年前
我下面的解決方案略有不正確,所以這是正確的版本 (我在漫長的一天結束時發布,這絕不是個好主意!)

同樣,這是一個快速且粗略的解決方案,可以阻止 mb_convert_encoding 在遇到目標編碼的非法字元時,在您的字串中填入問號。

<?php
function convert_to ( $source, $target_encoding )
{
// 偵測輸入檔案的字元編碼
$encoding = mb_detect_encoding( $source, "auto" );

// 跳脫所有問號,以便我們移除來自
// unicode 轉換過程的雜訊
$target = str_replace( "?", "[question_mark]", $source );

// 將字串轉換為目標編碼
$target = mb_convert_encoding( $target, $target_encoding, $encoding);

// 移除因非法字元而引入的任何問號
$target = str_replace( "?", "", $target );

// 將符記字串 "[question_mark]" 替換為符號 "?"
$target = str_replace( "[question_mark]", "?", $target );

return
$target;
}
?>

希望這對某人有幫助! (管理員可以隨意刪除我先前不正確的貼文,以求清晰)
-A
9
eion at bigfoot dot com
18 年前
以下許多人談論使用
<?php
mb_convert_encode
($s,'HTML-ENTITIES','UTF-8');
?>
來將非 ascii 程式碼轉換為 html 可讀取的內容。由於我的網頁伺服器不在我的控制範圍內,我無法設定資料庫字元集,而且每當 PHP 製作從資料庫提取的 $s 變數副本時,它都會自動將其轉換為難看的 latin1,而不是讓它保持美麗的 UTF-8 榮耀。

所以 [此處插入韓文字元] 變成了 ?????。

我發現自己需要使用傳址 (當然,這在最近的 PHP 版本中已棄用/不存在)
所以而不是
<?php
mb_convert_encode
(&$s,'HTML-ENTITIES','UTF-8');
?>
在升級之前,這可以完美運作,所以我必須使用
<?php
call_user_func_array
('mb_convert_encoding', array(&$s,'HTML-ENTITIES','UTF-8'));
?>

希望對其他人有幫助
3
Stephan van der Feest
19 年前
為了補充下面 Flash 轉換的註解,以下是我如何將從 Flash HTML 文字欄位輸出轉換後儲存在資料庫中的內容轉換回來,以便將其載回 Flash HTML 文字欄位

function htmltoflash($htmlstr)
{
return str_replace("&lt;br /&gt;","\n",
str_replace("<","&lt;",
str_replace(">","&gt;",
mb_convert_encoding(html_entity_decode($htmlstr),
"UTF-8","ISO-8859-1"))));
}
3
urko at wegetit dot eu
12 年前
如果您嘗試產生 CSV (具有擴充字元) 以在 Mac 的 Excel 中開啟,唯一對我有效的是
<?php mb_convert_encoding( $CSV, 'Windows-1252', 'UTF-8'); ?>

我也嘗試過這個

<?php
//分隔符號正確,字元錯誤
iconv('MACINTOSH', 'UTF8', $CSV);
//分隔符號錯誤,字元正確
chr(255).chr(254).mb_convert_encoding( $CSV, 'UCS-2LE', 'UTF-8');
?>

但第一個方法無法正確顯示擴展字元,第二個方法則無法正確分隔欄位。
1
me at gsnedders dot com
15 年前
似乎在處理未知的「來源編碼」時,函式會拋出 E_WARNING 錯誤,並繼續將字串從 ISO-8859-1 轉換為「目標編碼」。
1
vasiliauskas dot agnius at gmail dot com
6 年前
當您需要從 HTML 實體轉換時,如果您的 UTF-8 字串部分損壞(並非所有字元都是 UTF-8),在這種情況下,將字串傳遞給 mb_convert_encoding($string, 'UTF-8', 'HTML-ENTITIES'); 會使字串中的字元更加損壞。在這種情況下,您需要逐步替換 HTML 實體,以保留字元的良好編碼。我為此工作編寫了一個閉包函數。
<?php
$decode_entities
= function($string) {
preg_match_all("/&#?\w+;/", $string, $entities, PREG_SET_ORDER);
$entities = array_unique(array_column($entities, 0));
foreach (
$entities as $entity) {
$decoded = mb_convert_encoding($entity, 'UTF-8', 'HTML-ENTITIES');
$string = str_replace($entity, $decoded, $string);
}
return
$string;
};
?>
2
Daniel Trebbien
15 年前
請注意,`mb_convert_encoding($val, 'HTML-ENTITIES')` 不會跳脫 '\'', '"', '<', '>' 或 '&'。
1
katzlbtjunk at hotmail dot com
16 年前
透過簡單地將所有不需要的字元替換為底線 (ASCII 轉換為 7 位元),來清理字串以用作檔名。它會移除比必要更多的字元。希望這對您有所幫助。

$fileName = 'Test:!"$%&/()=ÖÄÜöäü<<';
echo strtr(mb_convert_encoding($fileName,'ASCII'),
' ,;:?*#!§$%&/(){}<>=`´|\\\'"',
'____________________________');
1
bmxmale at qwerty dot re
2 年前
/**
* 將 Windows-1250 轉換為 UTF-8
* 基於 https://php.dev.org.tw/manual/en/function.mb-convert-encoding.php#112547
*/
class TextConverter
{
private const ENCODING_TO = 'UTF-8';
private const ENCODING_FROM = 'ISO-8859-2';

private array $mapChrChr = [
0x8A => 0xA9,
0x8C => 0xA6,
0x8D => 0xAB,
0x8E => 0xAE,
0x8F => 0xAC,
0x9C => 0xB6,
0x9D => 0xBB,
0xA1 => 0xB7,
0xA5 => 0xA1,
0xBC => 0xA5,
0x9F => 0xBC,
0xB9 => 0xB1,
0x9A => 0xB9,
0xBE => 0xB5,
0x9E => 0xBE
];

private array $mapChrString = [
0x80 => '&euro;',
0x82 => '&sbquo;',
0x84 => '&bdquo;',
0x85 => '&hellip;',
0x86 => '&dagger;',
0x87 => '&Dagger;',
0x89 => '&permil;',
0x8B => '&lsaquo;',
0x91 => '&lsquo;',
0x92 => '&rsquo;',
0x93 => '&ldquo;',
0x94 => '&rdquo;',
0x95 => '&bull;',
0x96 => '&ndash;',
0x97 => '&mdash;',
0x99 => '&trade;',
0x9B => '&rsquo;',
0xA6 => '&brvbar;',
0xA9 => '&copy;',
0xAB => '&laquo;',
0xAE => '&reg;',
0xB1 => '&plusmn;',
0xB5 => '&micro;',
0xB6 => '&para;',
0xB7 => '&middot;',
0xBB => '&raquo;'
];

/**
* @param $text
* @return string
*/
public function execute($text): string
{
$map = $this->prepareMap();

return html_entity_decode(
mb_convert_encoding(strtr($text, $map), self::ENCODING_TO, self::ENCODING_FROM),
ENT_QUOTES,
self::ENCODING_TO
);
}

/**
* @return array
*/
private function prepareMap(): array
{
$maps[] = $this->arrayMapAssoc(function ($k, $v) {
return [chr($k), chr($v)];
}, $this->mapChrChr);

$maps[] = $this->arrayMapAssoc(function ($k, $v) {
return [chr($k), $v];
}, $this->mapChrString);

return array_merge([], ...$maps);
}

/**
* @param callable $function
* @param array $array
* @return array
*/
private function arrayMapAssoc(callable $function, array $array): array
{
return array_column(
array_map(
$function,
array_keys($array),
$array
),
1,
0
);
}
}
0
chzhang at gmail dot com
15 年前
您可以嘗試使用 mb_substitute_character("none"); 而不是 ini_set()。

mb_substitute_character("none");
0
lanka at eurocom dot od dot ua
21 年前
另一個在未啟用多位元組的情況下重新編碼的範例。
(俄文 koi->win,如果輸入已經是 win 編碼,則 recode() 函數會傳回未更改的字串)

<?php
// 0 - win
// 1 - koi
function detect_encoding($str) {
$win = 0;
$koi = 0;

for(
$i=0; $i<strlen($str); $i++) {
if(
ord($str[$i]) >224 && ord($str[$i]) < 255) $win++;
if(
ord($str[$i]) >192 && ord($str[$i]) < 223) $koi++;
}

if(
$win < $koi ) {
return
1;
} else return
0;

}

// recodes koi to win
function koi_to_win($string) {

$kw = array(128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 254, 224, 225, 246, 228, 229, 244, 227, 245, 232, 233, 234, 235, 236, 237, 238, 239, 255, 240, 241, 242, 243, 230, 226, 252, 251, 231, 248, 253, 249, 247, 250, 222, 192, 193, 214, 196, 197, 212, 195, 213, 200, 201, 202, 203, 204, 205, 206, 207, 223, 208, 209, 210, 211, 198, 194, 220, 219, 199, 216, 221, 217, 215, 218);
$wk = array(128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 225, 226, 247, 231, 228, 229, 246, 250, 233, 234, 235, 236, 237, 238, 239, 240, 242, 243, 244, 245, 230, 232, 227, 254, 251, 253, 255, 249, 248, 252, 224, 241, 193, 194, 215, 199, 196, 197, 214, 218, 201, 202, 203, 204, 205, 206, 207, 208, 210, 211, 212, 213, 198, 200, 195, 222, 219, 221, 223, 217, 216, 220, 192, 209);

$end = strlen($string);
$pos = 0;
do {
$c = ord($string[$pos]);
if (
$c>128) {
$string[$pos] = chr($kw[$c-128]);
}

} while (++
$pos < $end);

return
$string;
}

function
recode($str) {

$enc = detect_encoding($str);
if (
$enc==1) {
$str = koi_to_win($str);
}

return
$str;
}
?>
-1
nicole
8 年前
// 將 UTF8 轉換為 DOS = CP850
//
// $utf8_text=UTF8 格式化的文字;
// $dos=CP850 格式化的文字;

// 玩得開心

$dos = mb_convert_encoding($utf8_text, "CP850", mb_detect_encoding($utf8_text, "UTF-8, CP850, ISO-8859-15", true));
-1
Tom Class
19 年前
您為什麼要使用 PHP HTML 編碼函數? mbstring 有它自己的編碼,(就我測試過的而言) 更有用。

HTML-ENTITIES

範例

$text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
-1
Daniel
9 年前
如果您嘗試將 "UTF-8" 文字轉換為 "ISO-8859-1",且結果始終以 "ASCII" 傳回,請在 mb_convert_encoding 之前放置以下程式碼行:

mb_detect_order(array('UTF-8', 'ISO-8859-1'));

必須強制指定特定的搜尋順序才能使轉換正常運作。
-2
mac.com@nemo
18 年前
對於那些想要從 $set 轉換為 MacRoman 的人,請使用 iconv()。

<?php

$string
= iconv('UTF-8', 'macintosh', $string);

?>

('macintosh' 是 MacRoman 字元集的 IANA 名稱。)
-2
David Hull
17 年前
作為對 Johannes 將字串從其他字元集轉換為 7 位元表示法,而又不只是刪除拉丁語變音符號的建議的替代方案,您可以嘗試此方法:

<?php
$text
= iconv($from_enc, 'US-ASCII//TRANSLIT', $text);
?>

唯一的缺點是它不會將 "ä" 轉換為 "ae",但它能更好地處理標點符號和其他特殊字元。
--
David
-3
aofg
17 年前
在 PHP >= 5.2.1 上將日文字串轉換為 ISO-2022-JP 或 JIS 時,您可以使用 "ISO-2022-JP-MS" 來取代它們。
使用此編碼可以正確轉換機種依存字元(平台相依),與 eucJP-win 或 SJIS-win 相同。
-2
jamespilcher1 - hotmail
20 年前
從 iso-8859-1 轉換為 utf-8 時請小心。

即使您透過標頭和嚴格的 XML 定義明確指定頁面的字元編碼為 iso-8859-1,Windows 2000 也會忽略它,並將其解譯為其本機安裝的任何字元集。

例如,我使用字元編碼 iso-8859-1 將字元 #128 寫入頁面,它在 Internet Explorer (和 Mozilla) 中顯示為歐元符號。

它應該顯示一個方塊,表示字元 #128 在 iso-8859-1 中未定義。問題是它顯示在「Windows:西歐」(我的本機字元集) 中。

當我嘗試透過 mb_convert_encoding() 將此歐元轉換為 UTF-8 時,這導致了混亂。

IE 正確顯示 UTF-8 - 由於 PHP 正確地將 #128 轉換為 UTF-8 中的方塊,因此 IE 會顯示一個方塊。

因此,我所看到的只是 mb_convert_encoding() 將歐元符號轉換為方塊。我花了很長時間才弄清楚發生了什麼事。
-4
StigC
16 年前
對於像我一樣的 PHP 新手,使用 Flash 和 PHP。

這是一個簡單的程式碼片段,對我來說非常有效,讓 PHP 顯示來自 Flash 電子郵件表單的特殊丹麥字元。

<?php
// 名稱跳脫
$escName = mb_convert_encoding($_POST["Name"], "ISO-8859-1", "UTF-8");

// 訊息跳脫
$escMessage = mb_convert_encoding($_POST["Message"], "ISO-8859-1", "UTF-8");

// 標頭...等等...
?>
-4
nospam at nihonbunka dot com
16 年前
rodrigo at bb2 dot co dot jp 寫道,iconv 比 mb_convert_encoding 效果更好,我發現當從 uft8 轉換為 shift_jis 時。
$conv_str = mb_convert_encoding($str,$toCS,$fromCS);
有效,但
$conv_str = iconv($fromCS,$toCS.'//IGNORE',$str);
從 $str 中移除波浪符號。
-3
gullevek at gullevek dot org
14 年前
如果您想將日文轉換為 ISO-2022-JP,強烈建議您使用 ISO-2022-JP-MS 作為目標編碼。這包括擴展字元集,並避免文字中出現 ?。例如,經常使用的「圓圈中的 1」① 屆時將被正確轉換。
-3
rodrigo at bb2 dot co dot jp
16 年前
對於那些由於 PHP 版本較低而無法使用 mb_convert_encoding() 從一個字元集轉換為另一個字元集的人,請嘗試 iconv()。

我在轉換為日文字元集時遇到了這個問題

$txt=mb_convert_encoding($txt,'SJIS',$this->encode);

我可以使用這個方法修正它

$txt = iconv('UTF-8', 'SJIS', $txt);

也許這對其他人有幫助! ;)
-3
phpdoc at jeudi dot de
18 年前
我想分享一些程式碼,將拉丁字元的變音符號轉換為它們的
傳統 7 位元表示法,例如:

- &agrave;,&ccedil;,&eacute;,&icirc;,... 轉換為 a,c,e,i,...
- &szlig; 轉換為 ss
- &auml;,&Auml;,... 轉換為 ae,Ae,...
- &euml;,... 轉換為 e,...

(mb_convert "7bit" 只會簡單地刪除任何不合規的字元)。

我可能遺漏了您所在國家的印刷
慣例,請指正我。
&lt;?php
/**
* @args string $text 編碼文字行
* string $from_enc ( $text 的編碼類型,例如 UTF-8、ISO-8859-1)
*
* @returns 7 位元表示法
*/
function to7bit($text,$from_enc) {
$text = mb_convert_encoding($text,'HTML-ENTITIES',$from_enc);
$text = preg_replace(
array('/&szlig;/', '/&amp;(..)lig;/',
'/&amp;([aouAOU])uml;/', '/&amp;(.)[^;]*;/'),
array('ss',"$1","$1".'e',"$1"),
$text);
return $text;
}
?&gt;

請享用 :-)
Johannes

==
[由 danbrown AT php DOT net 編輯:作者於 2012 年 2 月 27 日提供了以下更新。]
==

針對我在註解中提及的 "to7bit" 函式的附錄。
此函式旨在解決某些語言需要對特殊(帶變音符號)字元進行不同的 7 位元呈現,以便進行排序或其他應用程式。例如,德語的 &szlig; 連字在 7 位元環境中通常寫作 "ss"。荷蘭語的 &yuml; 通常呈現為 "ij"(而不是 "y")。

原始函式對於單字(字母)字元實體效果很好,我已經在許多地方看到它被使用。但是非單字實體會產生有趣的結果
例如,"&copy;" 會呈現為 "c","&shy;" 會呈現為 "s",而 "&rquo;" 會呈現為 "r"。
以下版本通過將非字母數字字元(以及它們的鏈)轉換為 '_' 來修正此問題。

&lt;?php
/**
* @args string $text 編碼文字行
* string $from_enc ( $text 的編碼類型,例如 UTF-8、ISO-8859-1)
*
* @returns 7 位元表示法
*/
function to7bit($text,$from_enc) {
$text = preg_replace(/W+/,'_',$text);
$text = mb_convert_encoding($text,'HTML-ENTITIES',$from_enc);
$text = preg_replace(
array('/&szlig;/', '/&amp;(..)lig;/',
'/&amp;([aouAOU])uml;/', '/&yuml;/', '/&amp;(.)[^;]*;/'),
array('ss',"$1","$1".'e','ij',"$1"),
$text);
return $text;
}
?&gt;

再次享用,
Johannes
-4
Stephan van der Feest
19 年前
這是一個給任何使用 Flash 和 PHP 將 Flash 文字欄位提交的 HTML 輸出儲存到資料庫或其他地方的人的提示。

Flash 以 UTF-8 格式提交其 HTML 特殊字元,因此您可以使用以下函式將它們轉換為 HTML 實體字元

function utf8html($utf8str)
{
return htmlentities(mb_convert_encoding($utf8str,"ISO-8859-1","UTF-8"));
}
To Top