2011-03-05 66 views
0

我需要將希臘字母放入URL中,例如:www.example.com?id=σκ 我有這樣的字符串σκ:σκ 我可以在PHP中使用什麼函數將σκ轉換爲σκ?如何把σ κ轉換成σκ?

謝謝 phpheini

回答

1

使用html_entity_decode AND urlencode - 因爲你不應該把特殊的希臘字符放在url中。

$str = html_entity_decode('σκ', ENT_NOQUOTES, 'UTF-8'); 
// $str is 'σκ' 
$str = urlencode($str); 
// $str is '%CF%83%CE%BA' 

$link = 'http://www.example.com?id=' . $str; 
+0

第一個給我的:不能在html_entity_decode()中處理MBCS, ! 我猜也只能用於PHP 5 – phpheini 2011-03-05 00:40:38

+0

@phpheini,似乎你運行的是PHP的舊版本,請查看[comments](http://www.php.net/manual/de/function.html- entity-decode.php)的html_entity_decode可能的解決方案 - 我無法在我的php 5.3上測試它。 – Czechnology 2011-03-05 00:44:43

+0

你的意思是這個函數:'函數unhtmlentities($字符串) \t \t \t \t \t \t \t \t \t { \t \t \t \t \t \t \t \t \t \t //代替數字實體 \t \t \t \t \t \t \t \t \t \t $ string = preg_replace('〜&#x([0-9a-f] +);〜ei','chr(hexdec(「\\ 1」))',$ string); \t \t \t \t \t \t \t \t \t \t $字符串= preg_replace函數( '〜&#([0-9] +);〜E', 'CHR( 「\\ 1」)',$字符串); \t \t \t \t \t \t \t \t \t \t //替換字面實體 \t \t \t \t \t \t \t \t \t \t $ trans_tbl = get_html_translation_table(HTML_ENTITIES); \t \t \t \t \t \t \t \t \t \t $ trans_tbl = array_flip($ trans_tbl); \t \t \t \t \t \t \t \t \t \t回strtr函數的效率($字符串,$ trans_tbl); \t \t \t \t \t \t \t \t \t}' 這一次給了我一個非常加密結果。 – phpheini 2011-03-05 00:55:57

1

嘗試htmlspecialchars_decode(字符串$字符串[摘要$ quote_style = ENT_COMPAT])

編輯: 在這個網站上,你genereate的URL(參數)設置rawurlencode(字符串$ str)。然後url被編碼。然後你可以使用rawurldecode(string $ str)恢復它。

+0

這在我的測試中並沒有訣竅。 – Czechnology 2011-03-05 00:33:39

+0

不幸的是,這給了我一個錯誤消息調用未定義的函數(我猜這個函數只適用於PHP 5) – phpheini 2011-03-05 00:36:41

+0

但它沒有任何意義。當你發送id =σκ時,它不會逃過σ κ所以你不能將它轉換回來!你應該寫出你有php4(PHP 5> = 5.1.0)。 – Stony 2011-03-05 00:40:43