我有一个文本文件,我正在逐行阅读。当我到达字符串(六个字长)那一行时,我想读取它并将其分配给变量 $str,所以我这样做:
fscanf($handle, "%s", $str);
//The line is "one two three four"
echo $str ; // prints out "one"
但是,由于每个单词后面都有空格,它只保存字符串的第一个单词。我如何捕捉整个事物?
最佳答案
这是一个老问题,但对于 googlers:
To read a whole line with fscanf(), use "%[^\n]". In fscanf(), $[^characters] means to match any sequence of characters that isn't in the set between the brackes (it's similar to [^characters]* in a regular expression). So this matches any sequence of characters other than newline. See http://php.net/manual/en/function.sscanf.php#56076
关于php - 如何使用 fscanf 读取整行(字符串)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36389734/