【问题标题】:Compare first word of each line of a file to a variable in php将文件每一行的第一个单词与 php 中的变量进行比较
【发布时间】:2021-03-11 22:28:32
【问题描述】:

我刚刚开始使用 php,但我找不到一种简单的方法来比较格式为(“userid firstname lastname password”)的文本文件的一行的每个第一个单词的变量。

【问题讨论】:

  • 您认为什么“容易”?打开文件,逐行读取并提取第一个单词。您对以下哪些步骤有疑问?
  • 我在逐行读取文件并提取第一个单词时遇到问题。所以除了打开文件之外的一切。
  • this 对第一部分有帮助吗? this 和第二个?

标签: php string-search


【解决方案1】:
<?php
     $mf = fopen("users.txt", "r") or die("Unable to open user database file!");
     $fc = file_get_contents('users.txt');
     $cv = "userid";
     if($fc == $cv) {
          //the contents of the file contain userid
     }
     fclose($mf);
?>

这并不完全是您想要的,但它应该会引导您朝着正确的方向前进。您将不得不考虑解析每行的第一个单词并将其与变量进行比较。用解析替换 if 语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多