2013-12-10 3 views
0

file_get_contents 및 fwrite에 문제가 있습니다.php - 잘못된 문자 쓰기

스크립트를 작동 시키려면 외부 URL의 내용을 html 파일로 인쇄해야합니다. 내가 file_get_contents를 에코 때

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
</head> 
<body> 

<?php 
$url = 'http://www.vasttrafik.se/nasta-tur-fullskarm/?externalid=9021014005135000'; 
$content = file_get_contents($url); 
echo $content; // Actually writes out correct 

$myFile = "response.php"; 
$fh = fopen($myFile, 'w') or die("can't open file"); 
fwrite($fh, $content); // Doesn't write out correct ??? 
fclose($fh); 
?> 

</body> 
</html> 

에서, HTML을 능숙하게 보여줍니다 (스웨덴 특수 문자 : AAO)

그러나 .. 파일

이 코드를 사용하고 있습니다 "response.php"는 åäö 대신 잘못된 문자를 표시합니다.

아이디어가 있으십니까? fwrite가 다른 인코딩을 사용합니까? 감사합니다.

업데이트! 이 함께 해결 :

$content = "\xEF\xBB\xBF"; 
$content .= utf8_encode(file_get_contents($url)); 
+0

http://stackoverflow.com/questions/13932519/utf-8-characters-in-fwrite – Aborted

+0

정확히 어떻게 당신이 그 파일의 결과를 확인됩니다? – deceze

+0

@Dugi : 도움이되지 않았 음 :/ deceze : $ content를 표시하면 작동합니다. 하지만 "response.php"서핑을 할 때 "åäö"는 이상합니다 .. 그리고 네,이 스크립트를 실행하면 response.php 파일이 업데이트됩니다. – Stichy

답변

1

해결!

BOM (Byte Order Mark) 및 utf8_encode를 광고해야했습니다. 이처럼

:

$content = "\xEF\xBB\xBF"; 
$content .= utf8_encode(file_get_contents($url)); 
+1

당신이 한 일은 당신이 무엇이든간에 출력물을 호환 가능하게 만드는 것입니다 이 파일을 읽는 데 사용하면 파일의 데이터가 잘못 쓰여진 적이 없습니다. – symcbean