file_get_contents() UTF-8 문자를 분할합니다.
외부 서버에서 HTML을 로드하고 있습니다.HTML 마크업에는 UTF-8 인코딩이 있으며 ", sh, ch,", j 등의 문자가 포함되어 있습니다.HTML에 file_get_contents()를 로드하면 다음과 같이 됩니다.
$html = file_get_contents('http://example.com/foreign.html');
UTF-8 문자를 혼동하여 적절한 UTF-8 문자가 아닌 ,, and, and 등의 넌센스를 로드합니다.
어떻게 하면 해결할 수 있을까요?
갱신:
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
따라서 다른 문제로 이어질 수 있습니다.
예:
$string = file_get_contents(".../File.txt");
$string = mb_convert_encoding($string, 'UTF-8', "ISO-8859-1");
echo $string;
터키어, mb_convert_encoding 또는 기타 문자 집합 변환이 작동하지 않았습니다.
또한 스페이스 char가 + char로 변환되어 urlencode가 동작하지 않았습니다.퍼센트 인코딩의 경우 %20이어야 합니다.
이거 먹혔어!
$url = rawurlencode($url);
$url = str_replace("%3A", ":", $url);
$url = str_replace("%2F", "/", $url);
$data = file_get_contents($url);
아래의 기능을 사용하여 해결할 수 있었습니다.
function file_get_contents_utf8($url) {
$content = file_get_contents($url);
return mb_convert_encoding($content, "HTML-ENTITIES", "UTF-8");
}
file_get_contents_utf8($url);
이것도 먹어봐
$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);
저는 35000라인의 데이터로 작업하고 있습니다.
$f=fopen("veri1.txt","r");
$i=0;
while(!feof($f)){
$i++;
$line=mb_convert_encoding(fgets($f), 'HTML-ENTITIES', "UTF-8");
echo $line;
}
이 코드는 나의 이상한 문자를 정상 문자로 변환한다.
저도 비슷한 문제가 있었는데 해결한 건html_entity_decode
.
암호는 다음과 같습니다.
$content = file_get_contents("http://example.com/fr");
$x = new SimpleXMLElement($content);
foreach($x->channel->item as $entry) {
$subEntry = html_entity_decode($entry->description);
}
여기서 xml 파일(프랑스어)을 취득하고 있기 때문에 이 $x 오브젝트 변수를 사용하고 있습니다.그 다음에야 이 변수로 복호화해서$subEntry
.
나는 노력했다.mb_convert_encoding
하지만 이건 통하지 않았다.
이 기능을 사용해 보세요.
function mb_html_entity_decode($string) {
if (extension_loaded('mbstring') === true)
{
mb_language('Neutral');
mb_internal_encoding('UTF-8');
mb_detect_order(array('UTF-8', 'ISO-8859-15', 'ISO-8859-1', 'ASCII'));
return mb_convert_encoding($string, 'UTF-8', 'HTML-ENTITIES');
}
return html_entity_decode($string, ENT_COMPAT, 'UTF-8');
}
언급URL : https://stackoverflow.com/questions/2236668/file-get-contents-breaks-up-utf-8-characters
'programing' 카테고리의 다른 글
Python 3.5의 타입 힌트는 무엇입니까? (0) | 2022.09.28 |
---|---|
sql.gz 파일을 가져와 putty를 사용하여 데이터베이스에 삽입합니다. (0) | 2022.09.28 |
오브젝트 메모리주소에의 액세스 (0) | 2022.09.28 |
jQuery를 사용하여 URL로 이동하는 방법 (0) | 2022.09.28 |
MariaDB 생성 뷰가 SELECT를 다른(잘못된) 쿼리로 변경합니다. (0) | 2022.09.28 |