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

标签 php string format scanf

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

相关文章:

php - 我如何像在 Ruby 中使用 block 一样使用 PHP 5.3 闭包

android - 我们可以在 values 文件夹中为 sting strings (Android) 创建 new_strings.xml 和 strings.xml 吗?

numbers - 在 Twig 模板中格式化金钱

Java,如何将 GMT 时间更改为本地时间?

php - PHP 中的 sprintf 长度说明符

php - XMPP (eJabberd) 如何为浏览器工作

php - 我可以在没有 javascript 的情况下只加载页面的一部分吗?

java - 如何将字符串映射到 Java 中的函数?

swift - 对 Swift 中枚举的误解

c# - 为货币格式化文本框字符串