Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8178843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:51:52+00:00 2026-06-06T23:51:52+00:00

How can I, from a text file, use explode() to take the 1st 2

  • 0

How can I, from a text file, use explode() to take the 1st 2 items (using comma as the delimiter) in each line ?


Username check is working.
The email however isn’t.
The countEmail doesn’t change.

This is the output I get:
(Tried to add an existing email and the countEmail didn’t change)

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)

And this is the code I use:

<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;

?>

I apologize if I sound confusing.
Also, I know there are probably much better ways of doing this, but I just started learning php 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:51:53+00:00Added an answer on June 6, 2026 at 11:51 pm

    The php function fgets will read in the file line by line, allowing you to then process and do explodes on a line by line basis.

    From the manual:

    <?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);
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading lines from a text file using (line-seq (reader input.txt)) . This collection
Is there some way I can take text (retrieved from a form), and email
We can't use java.text.SimpleDateFormat, so is there any way to create date object from
I am trying to use a textreader to retrieve data from a text file
I'm trying to use urllib and urllib2 to read from a text file that
This code will read a line from a text file: set file = CreateObject(Scripting.FileSystemObject).OpenTextFile(c:\number.txt,
I can load a matrix from text file: load mydata.txt The problem is my
I am attempting to read hex values from a text file. There can be
I am trying to delete a line from a text file in java, I
I'm trying to find and read a string from a text file using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.