在PHP中读取文件的一部分时的多字节指针 [英] Multibyte pointer when reading part of a file in PHP
问题描述
我使用PHP.
下面的函数加载一个大的多字节部分,输入一个单独的CSV文件,并在数组中返回pointer
(结束位置)和content
.使用pointer
,我以后可以再次运行.它的工作原理:
The function below loads part of a big multibyte enter separated CSV file and return a pointer
(the end position) and the content
in an array. With the pointer
I can later do another run. It works:
function part($path, $offset, $rows) {
$buffer = array();
$buffer['content'] = '';
$buffer['pointer'] = array();
$handle = fopen($path, "r");
fseek($handle, $offset);
if( $handle ) {
for( $i = 0; $i < $rows; $i++ ) {
$buffer['content'] .= fgets($handle);
$buffer['pointer'] = mb_strlen($buffer['content']);
}
}
fclose($handle);
return($buffer);
}
// Buffer first part
$buffer = part($path_to_file, 0, 100);
// Buffer second part
$buffer = part($path_to_file, $buffer['pointer'], 100);
print_r($buffer);
如果我将$buffer['pointer']
行更改为:
If I change the $buffer['pointer']
line to:
$buffer['pointer'] = mb_strlen($buffer['content'], "UTF-8");
...它不再起作用...我知道当我使用UTF-8而不是默认值时,它将使用不同的编码,但是为什么它不能与UTF-8一起使用?
...it does not work anymore... I understand that it uses the different encoding when I use UTF-8 instead of the default, but why doesn't it work with UTF-8?
UTF-8是否应与外来字符兼容?
因为当我在不使用"UTF-8"的情况下使用上述功能时,我想我可以在不使用UTF-8的情况下使用它.
Because the function above works when I use it without "UTF-8" I guess I could just use it without UTF-8.
我仍然担心在某些情况下它会给出错误的指针?
是否有更安全的方法来获取正确的指针?
执行此操作时,我得到UTF-8
:
When I do this I get UTF-8
:
echo mb_detect_encoding($buffer['content']);
推荐答案
这与UTF-8无关.文件系统功能(如fseek()
,fread()
等)对单个字节进行操作.他们根本不关心编码. (您可能正在写入/读取二进制数据).
This has little to do with UTF-8. Filesystem functions (like fseek()
, fread()
, etc.) operate on individual bytes. They don't care about the encoding at all. (You could be writing / reading binary data).
如果以后要存储指向fseek()
的指针,请使用 ftell()
找出当前位置:
If you want to store a pointer to fseek()
to at a later time, use ftell()
to find out the current position:
$buffer['pointer'] = ftell($handle);
这篇关于在PHP中读取文件的一部分时的多字节指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!