인코딩 관련 질문
본문
<?php | |
header("Content-Type: text/vtt; charset=utf-8"); | |
function file_get_contents_utf8($fn) { | |
$content = file_get_contents($fn); | |
return $content; | |
} | |
function samiTime2vttTime($samiTime) { | |
$sec = fmod($samiTime / 1000, 60); | |
$min = $samiTime / 1000 / 60 % 60; | |
$hour = $samiTime / 1000 / 60 / 60; | |
return sprintf("%02d:%02d:%06.3f", $hour, $min, $sec); | |
} | |
function arr_del($list_arr, $del_num) { | |
$key = array_search($del_num, $list_arr); | |
array_splice($list_arr, $key, 1); | |
return $list_arr; | |
} | |
$filename = "./" .urldecode($_GET['name']); | |
if (file_exists($filename)) { | |
$beginTime = 0; | |
$endTime = 0; | |
$index = 0; | |
$file = fopen($filename, "r") or die("파일열기에 실패하였습니다"); | |
//$filestring = preg_split('/\n/i', $filestrings); | |
$str = file_get_contents_utf8($filename); | |
echo "WEBVTT\n\n"; | |
if (strtolower(substr($filename, -3)) !== 'smi') { | |
return; | |
} | |
$pattern = "/<SYNC[^>]*Start=(?<time>\d*)[^>]*><P[^>]*Class=(?<lang>\w*)[^>]*>/i"; | |
$ary = array(); | |
$ary[] = "ASCII"; | |
$ary[] = "JIS"; | |
$ary[] = "WINDOWS-1252"; | |
$ary[] = "EUC-KR"; | |
$ary[] = "SJIS-WIN"; | |
$ary[] = "UTF-8"; | |
$ary[] = "CP1252"; | |
$CharCheck = mb_detect_encoding($str, $ary); | |
if ($CharCheck == false) { | |
$adssadf = "00:00:00.000 --> 24:00:00.000\r\n자막에 문제가 발견되었습니다.\r\n(Encoding Find Fail : ".$CharCheck.")"; | |
echo mb_convert_encoding($adssadf, "UTF-8", mb_detect_encoding($adssadf, $ary)); | |
} | |
if ($CharCheck != "UTF-8"){ | |
$str = mb_convert_encoding($str, "UTF-8", $CharCheck); | |
} | |
$str = str_replace("\r\n","", $str); | |
$str = preg_replace("/<\/body>|<\/sami>/i", "", $str); | |
$pattern = "/<SYNC[^>]*Start=(?<time>\d*)[^>]*><P[^>]*Class=(?<lang>\w*)[^>]*>/i"; | |
$strz = preg_split($pattern, $str); | |
$strzf = preg_match_all($pattern, $str, $out); | |
$strz = arr_del($strz, 0); | |
$size = count($strz); | |
for ($i = 0; $i < $size - 1; $i++) { | |
$istwowrite = false; | |
$message = $strz[$i]; | |
echo samiTime2vttTime($out['time'][$i])." --> ".samiTime2vttTime($out['time'][$i+1])."\r\n"; | |
// 엔터시작 | |
if (preg_match("/<br>|<br\/>/i", $message)) { | |
$istwowrite = true; | |
$message = preg_replace("/<br>|<br\/>/i", "\r\n", $message); | |
} | |
if ($istwowrite == false) { | |
$message = " \r\n".$message; | |
} | |
// 엔터완료 | |
// 색깔시작 | |
$message = preg_replace("/<\/font>/i", "</c>", $message); // </font> 태그 </c> 변경 | |
$fontcolor_preg = "/<font[^>]*color=(?<color>.+?)(?=>|$)*>/i"; | |
$fontcolor = preg_match_all($fontcolor_preg, $message, $font_out); | |
$fontsize = count($font_out); | |
for ($y = 0; $y < $fontsize; $y++) { | |
if (array_key_exists($y, $font_out['color'])) { | |
$colorname = str_replace("\"", "", $font_out['color'][$y]); | |
$colorname = str_replace("#", "", $colorname); | |
$message = str_replace($font_out[0][$y], "<c.color-".strtolower($colorname).">", $message); | |
} | |
} // 색깔 | |
echo $message."\r\n\r\n"; | |
} | |
} | |
?> |
WEBVTT 00:00:00.000 --> 24:00:00.000 자막에 문제가 발견되었습니다. (Encoding Find Fail : )<br /> <b>Warning</b>: mb_convert_encoding(): Illegal character encoding specified in <b>C:\xampp\htdocs\convert_vtt.php</b> on line <b>50</b><br />
위와 같은 에러가 뜹니다. iconv해도 마찬가진데 컴퓨터 문제 인가요?
XAMPP 사용중이며 php 7.4버전으로 알고 있습니다.
고수 여러분 도와 주세요.
답변 3
mb_detect_encoding() 함수에 들어가는 $str이 화면에 어떻게 나타나는지는 확인 해보셨을까요?
지금 제 예상으로는 저기에 나타나는 글자가 $ary에 없는것 같다 입니다.
위에꺼를 확인 해보셨으면 현재 php에서 설정된 인코딩도 확인 해보시길 바랍니다.
Detected an illegal character in input string - 입력된 문자열에 EUC-KR 문자가 아닌 것이 발견되었다는 뜻인데요... 자막파일 자체에 문제가 있는 것 같습니다. 다른 자막파일로 테스트해보심이 어떨련지요?
그럼 mb_detect_encoding($str, $ary); 대신 mb_detect_encoding($str); 이렇게 해서 결과값이 어떻게 나오는지 확인해보세요. 지정한 인코딩중에 없는 인코딩이 있는 것 같습니다.
답변을 작성하시기 전에 로그인 해주세요.