1

如何从文本文件中使用 explode() 在每行中获取第 2 项(使用逗号作为分隔符)?


用户名检查正在工作。
然而,电子邮件不是。
countEmail 没有改变。

这是我得到的输出:(
试图添加一个现有的电子邮件并且 countEmail 没有改变)

array(3) {    <--- Previous user
  [0]=>
  string(11) "blabla123"
  [1]=>
  string(28) " blahblah@blah.com"
  [2]=>
  string(11) " 123, 1990
"
}
array(1) {
  [0]=>
  string(1) "
"
}

Notice: Undefined offset: 1 in C:\xampp\htdocs\Brets\register.php on line 35

Notice: Undefined offset: 1 in C:\xampp\htdocs\Brets\register.php on line 36
00 (countUser and countEmail)

这是我使用的代码:

<form action="register.php" method="POST">
    Username: <br> <input type="text" name="username"> <br>
    Password: <br> <input type="password" name="password"> <br>
    Email: <br> <input type="text" name="email"> <br>
    Year of Birth: <br> <select name="year">
                            <option value="1990">1990</option>
                            <option value="1991">1991</option>
                            <option value="1992">1992</option>
                            <option value="1993">1993</option>
                        </select> <br>
    <input type="submit" value="Submit">
</form>

<?php
$next = 'register.php';
$greet = 'Welcome! <p>Please register using the form above.</p>';

if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email']) && isset($_POST['year'])) {
    $username = htmlentities($_POST['username']);
    $password = htmlentities($_POST['password']);
    $email = htmlentities($_POST['email']);
    $year = htmlentities($_POST['year']);

    if (!empty($username) && !empty($password) && !empty($email) && !empty($year)) {
        $countEmail = 0;
        $countUser = 0;
        $filename = 'user.txt';
        $handle = fopen($filename, "a+");
        if ($handle) {
            while (($line = fgets($handle, filesize($filename))) !== false) {
                $line=explode(',', $line, 3);
                echo '<pre>';
                var_dump($line);
                echo '</pre>';
                if (($line[0] == $email) || ($line[1] == $email)) { $countEmail++; }
                if (($line[0] == $username) || ($line[1] == $username)) { $countUser++; }
            }
            if (!feof($handle)) {
                echo "Error: unexpected fgets() fail\n";
            }
        }
        if ($countEmail == 0 && $countUser == 0) {
            fwrite($handle, "$username, $email, $password, $year\r\n");
            fclose($handle);
            $greet = 'Thank you!';
        }
        if ($countEmail != 0) {$greet = 'An account with that email aready exists.';}
        if ($countEmail != 0) {$greet = 'Username already taken.';}
    } else { $greet = 'Fill in all fields.';}
}
echo $greet;

?>

如果我听起来令人困惑,我深表歉意。
另外,我知道可能有更好的方法可以做到这一点,但我刚刚开始学习 php :)

4

4 回答 4

2

php 函数fgets将逐行读取文件,然后允许您逐行处理和分解。

从手册:

<?php
$handle = @fopen($filename, "r");
if ($handle) {
    while (($line = fgets($handle, 4096)) !== false) {
        // do something with $line here
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}
?>
于 2012-07-04T15:39:13.457 回答
1

应该很简单:

list($item1, $item2) = explode(',', $str);

用于逐行fgets()读取。

顺便说一句,如果数据是逗号分隔的,你也可以看看fgetcsv()

于 2012-07-04T15:37:35.863 回答
1
$dataLines = explode("\n", $dataIn);

...

foreach ($dataLines as $line) {
  $line=explode(',', $line,3);
  if (siezof($line)<2) continue;
  if (($line[0] == $email) || ($line[0] == $email)) { $countEmail++; }
  if (($line[0] == $username) || ($line[0] == $username)) { $countUser++; }
}
于 2012-07-04T15:42:38.873 回答
0

在explode 中使用限制将留下包含其余字符串的最后一个元素。

http://php.net/manual/en/function.explode.php

所以如果你只想要前两个元素,

例如,从 "a,b,c,d" 你想要 ["a", "b"] 使用:

$result = explode(",", $array, 3) 
=> ["a", "b", "c,d"]

然后弹出删除最后一个:

array_pop($result);
print_r($result);
=> ["a", "b"]
于 2012-07-04T15:42:51.240 回答