PHP:从txt文件中读取用户和密码

时间:2012-03-08 03:45:02

标签: php file file-io

我有一个看起来像这样的txt文件

  

USER:user46226234   通过:#)(HF(WE * FHWE

     

USER:user49503420594   通过:黄鸟

     

USER:user449202934   通过:狗

我正在尝试提取这些对中的每一对并在脚本中使用它们。我不是一个真正的PHP开发人员,在该领域几乎没有技能,但我可以用我的方式处理函数,我知道基本语法。

提前感谢您的关注。

3 个答案:

答案 0 :(得分:4)

假设您的密码文件仅包含有效行和空行

编辑:如果您的php配置严格:

$lines = file('file.txt');
$credentials = array();

foreach($lines as $line) {
    if(empty($line)) continue;

    // whole line
    $line = trim(str_replace(": ", ':', $line));
    $lineArr = explode(' ', $line);

    // username only
    $username = explode(':', $lineArr[0]);
    $username = array_pop($username);

    // password
    $password = explode(':', $lineArr[1]);
    $password = array_pop($password);

    // putting them together
    $credentials[$username] = $password;
}

print_r($credentials);

答案 1 :(得分:0)

查看本教程,我认为写得很好。 http://www.tizag.com/phpT/fileread.php

答案 2 :(得分:0)

您可以尝试file_get_contents()查看here

如果您想使用配置文件,例如.ini文件,您还要检查parse_ini_files()检查here