PHP - 在文件中为每行使用一次explode()

时间:2012-07-04 15:34:03

标签: php file explode

我如何从文本文件中使用explode()来获取每行中的前两项(使用逗号作为分隔符)?


用户名检查正常。
然而,电子邮件不是。
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 个答案:

答案 0 :(得分: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);
}
?>

答案 1 :(得分:1)

应该如此简单:

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

使用fgets()逐行阅读。

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

答案 2 :(得分: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++; }
}

答案 3 :(得分:0)

在爆炸中使用限制将使最后一个元素包含其余字符串。

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

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

e.g。来自“a,b,c,d”你想要[“a”,“b”]使用:

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

然后弹出以删除最后一个:

array_pop($result);
print_r($result);
=> ["a", "b"]