我正在使用这段代码和类似的东西来解析文件......
if ($dataLines[0] == "0 HEAD" &&
($dataLines[count($dataLines) - 1] == "0 TRLR" ||
$dataLines[count($dataLines) - 2] == "0 TRLR")) {
// More Code Here
}
我添加了以下其他用于调试...
} else {
$this->error("import(): File is not a gedcom datafile: " . $filename);
$this->debug("import(): Lines: " . count($dataLines));
$this->debug("import(): Lines: dataLines[0] = [" . $dataLines[0] ."]");
$this->debug("import(): Lines: dataLines[count($dataLines) - 1] = [" . $dataLines[count($dataLines) - 1] ."]");
}
当我解析 ANSII 文件时,一切正常。我得到了一个 UTF-8 格式的文件,但事情已经中断。我的输出是:
Starting gedcom read
import(): File is not a gedcom datafile: /Users/jzaun/Development/www/assets/trees/greek/tree.ged
import(): Lines: 10712
import(): Lines: dataLines[0] = [0 HEAD ]
我也得到一个错误:
PHP 致命错误:在 /Users/jzaun/Development/www/classes/App/Gedcom.php:478 中包含消息“数组到字符串转换”的未捕获异常“ErrorException”堆栈跟踪:
要加载我正在使用的文件:
function file_get_contents_utf8($fn) {
$content = file_get_contents($fn);
return mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content));
}
$data = $this->file_get_contents_utf8($filename);
$dataLines = explode("\n", trim($data));
if (count($dataLines) == 1) {
$dataLines = explode("\r", trim($data));
}
我猜我要么加载文件错误,要么我不应该做类似$dataLines[0] == "0 HEAD"
. 我应该如何解析文件以使其与 UTF-8 一起使用?