gpt4 book ai didi

php - 如何使用 fscanf 读取整行(字符串)?

转载 作者:搜寻专家 更新时间:2023-10-31 21:25:50 26 4
gpt4 key购买 nike

我有一个文本文件,我正在逐行阅读。当我到达字符串(六个字长)那一行时,我想读取它并将其分配给变量 $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 vs fgets

关于php - 如何使用 fscanf 读取整行(字符串)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36389734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com