2

多年来,我一直在使用 C++ 文件流,并且可以从这样的文件中加载字符串:

char a[30],b[30],c[30];
ofstream fp;
fp.open("file.txt");
fp<<a<<b<<c;

有没有办法以相同的方式从 PHP 文件中读取字符串而不读取整行或文件?

4

1 回答 1

1

我想你正在寻找fscanf

$handle = fopen("users.txt", "r");
while ($userinfo = fscanf($handle, "%s\t%s\t%s\n")) {
    list ($name, $profession, $countrycode) = $userinfo;
    //... do something with the values
}

一个明显的区别是您必须将格式指定为函数参数(在使用动态类型时很难进行反思)。但是,优势在于,您可能会非常具体地使用诸如此类的格式来接收哪些字符%[0-9]|%[a-zA-Z0-9@&;:,. /!?-]

于 2013-09-29T21:57:40.463 回答