IT TIP

file_get_contents ()가 UTF-8 문자를 분리합니다.

itqueen 2020. 12. 9. 22:01
반응형

file_get_contents ()가 UTF-8 문자를 분리합니다.


외부 서버에서 HTML을로드하고 있습니다. HTML 마크 업은 UTF-8 인코딩을 가지며 ľ, š, č, ť, ž 등과 같은 문자를 포함합니다. 다음과 같이 file_get_contents ()를 사용하여 HTML을로드 할 때 :

$html = file_get_contents('http://example.com/foreign.html');

UTF-8 문자를 엉망으로 만들고 적절한 UTF-8 문자 대신 Å, ¾, ¤ 및 유사한 넌센스를로드합니다.

어떻게 해결할 수 있습니까?

최신 정보:

HTML을 파일에 저장하고 UTF-8 인코딩으로 출력 해 보았습니다. 둘 다 작동하지 않으므로 file_get_contents ()가 이미 손상된 HTML을 반환하고 있음을 의미합니다.

업데이트 2 :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="sk" lang="sk">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Language" content="sk" />
<title>Test</title>

</head>
<body>


<?php

$html = file_get_contents('http://example.com');
echo htmlentities($html);

?>

</body>
</html>

나는 폴란드어와 비슷한 문제가 있었다

나는 시도했다 :

$fileEndEnd = mb_convert_encoding($fileEndEnd, 'UTF-8', mb_detect_encoding($fileEndEnd, 'UTF-8', true));

나는 시도했다 :

$fileEndEnd = utf8_encode ( $fileEndEnd );

나는 시도했다 :

$fileEndEnd = iconv( "UTF-8", "UTF-8", $fileEndEnd );

그리고 -

$fileEndEnd = mb_convert_encoding($fileEndEnd, 'HTML-ENTITIES', "UTF-8");

이 마지막은 완벽하게 작동했습니다 !!!!!!


file_get_contents에 대한 PHP 매뉴얼 항목의 주석에서 제안 된 솔루션

function file_get_contents_utf8($fn) {
     $content = file_get_contents($fn);
      return mb_convert_encoding($content, 'UTF-8',
          mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}

http://php.net/manual/en/function.mb-internal-encoding.php로 운을 시험해 볼 수도 있습니다 .


좋구나. file_get_contents () 가이 문제를 일으키지 않는다는 것을 알았습니다. 다른 질문에서 제가 말하는 다른 이유가 있습니다. 바보 나.

이 질문을 참조하십시오 : DOM이 인코딩을 변경하는 이유는 무엇입니까?


나는 당신이 단순히 문자 유형의 이중 변환을 가지고 있다고 생각합니다 : D

html 문서 내에서 html 문서를 열었 기 때문일 수 있습니다. 그래서 결국 이렇게 보이는 것이 있습니다.

<!DOCTYPE html> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
<!DOCTYPE html> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test</title>.......

따라서를 사용 mb_detect_encoding하면 다른 문제가 발생할 수 있습니다.


이것도 시도

 $url = 'http://www.domain.com/';
    $html = file_get_contents($url);

    //Change encoding to UTF-8 from ISO-8859-1
    $html = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $html);

터키어, mb_convert_encoding 또는 기타 문자 집합 변환이 작동하지 않았습니다.

또한 urlencode는 공백 문자가 + 문자로 변환되어 작동하지 않았습니다. 백분율 인코딩의 경우 % 20이어야합니다.

이것은 작동했습니다!

   $url = rawurlencode($url);
   $url = str_replace("%3A", ":", $url);
   $url = str_replace("%2F", "/", $url);

   $data = file_get_contents($url);

35000 줄의 데이터로 작업하고 있습니다.

$f=fopen("veri1.txt","r");
$i=0;
while(!feof($f)){
    $i++;
    $line=mb_convert_encoding(fgets($f), 'HTML-ENTITIES', "UTF-8");
    echo $line;
}

이 코드는 이상한 문자를 정상으로 변환합니다.


예 :

$string = file_get_contents(".../File.txt");
$string = mb_convert_encoding($string, 'UTF-8', "ISO-8859-1");
echo $string;

참고URL : https://stackoverflow.com/questions/2236668/file-get-contents-breaks-up-utf-8-characters

반응형