2011-08-09 20 views
1

我試圖從維基百科得到這個刮圖像。 免費授權媒體有什麼好處如果你不能得到它?原始腳本是herephp file_put_contents亞洲字符文件名編碼

如果你把這個

http://upload.wikimedia.org/wikipedia/commons/2/26/%E7%9A%84-bw.png

在Firefox

,它會立即轉化成

http://upload.wikimedia.org/wikipedia/commons/ 2/26 /的-bw.png

以便保存圖像時將其保存爲-bw.png

足夠簡單了嗎?現在如何讓PHP來做到這一點?只是猜測,我試過utf8_decode($文件名)..但得到錯誤的中文字符。

$src= "http://upload.wikimedia.org/wikipedia/commons/2/26/%E7%9A%84-bw.png"; 
$pngData = file_get_contents($src); 
$fileName = basename($src); 
file_put_contents($fileName, $pngData); 

任何幫助表示讚賞,因爲我真的不知道該從哪裏出發。

回答

1

你試過url_decode();

<?php 
$url = 'http://upload.wikimedia.org/wikipedia/commons/2/26/%E7%9A%84-bw.png'; 
$parts = explode('/', $url); 
$title = $parts[count($parts)-1]; //get last section 

$title = urldecode($title); 
?> 
+0

BRR ..是的,試過了,仍然得到了錯誤的字符。我從$ src中複製url,粘貼到firefox中,然後使用正確的文件名獲取文件。用PHP,我得到了相同的文件,但有不同的文件名,另一箇中文字符。更新了問題.. – Slabo

+0

來思考它,我想你已經回答了我的問題。至少讓我指向正確的方向。我將重新解釋這個問題併發佈一個新的。**它是urldecode()而不是url_decode();順便說一下** – Slabo

+0

所以我會做的是獲得它的標題部分,然後在該部分運行urldecode。檢查上面編輯過的代碼(不知道它是否會工作,沒有測試,然後確保你使用的是utf8或類似的東西,這樣字符不會與ASCII字符混淆 – Ben

0

的squirrelmail包含在源一個不錯的功能,以Unicode字符轉換成實體:

<?php 
function charset_decode_utf_8 ($string) { 
     /* Only do the slow convert if there are 8-bit characters */ 
    /* avoid using 0xA0 (\240) in ereg ranges. RH73 does not like that */ 
    if (! ereg("[\200-\237]", $string) and ! ereg("[\241-\377]", $string)) 
     return $string; 

    // decode three byte unicode characters 
    $string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e",   
    "'&#'.((ord('\\1')-224)*4096 + (ord('\\2')-128)*64 + (ord('\\3')-128)).';'",  
    $string); 

    // decode two byte unicode characters 
    $string = preg_replace("/([\300-\337])([\200-\277])/e", 
    "'&#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'", 
    $string); 

    return $string; 
} 
?> 
+0

謝謝你。試過了,返回與urldecode相同的字符以上。 – Slabo

相關問題