简体   繁体   中英

PHP Reading Lines from a Text File

I am trying to search a line in a text file and then print the following three lines. For example, if the text file has

1413X
Peter
858-909-9999
123 Apple road

then my PHP file would take in an ID ("1413X") through a form, compare it to lines in the text file - essentially a mock database - and then echo the following three lines. Currently, it is echoing only the phone number (with the second half of the numbers wrong??). Thanks for your help.

<?php
    include 'SearchAddrForm.html';

    $file = fopen("addrbook.txt", "a+");
    $status = false;
    $data = '';


    if (isset($_POST['UserID']))
    {
        $iD = $_POST['UserID'];
        $contact = "";

        rewind($file);

        while(!feof($file))
        {
            if (fgets($file) == $iD)
            {
                $contact = fgets($file);
                $contact += fgets($file);
                $contact += fgets($file);
                break;
            }
        }

        echo $contact;
    }

    fclose($file);
?>

What I did:

<?php

//input (string)
$file = "before\n1413X\nPeter\n858-909-9999\n123 Apple road\nafter";

//sorry for the name, couldn't find better
//we give 2 strings to the function: the text we search ($search) and the file ($string)
function returnNextThreeLines($search, $string) {

    //didn't do any check to see if the variables are not empty, strings, etc

    //turns the string into an array which contains each lines
    $array = explode("\n", $string);

    foreach ($array as $key => $value) {
        //if the text of the line is the one we search
        //and if the array contains 3 or more lines after the actual one
        if($value == $search AND count($array) >= $key + 3) {
            //we return an array containing the next 3 lines
            return [
                $array[$key + 1],
                $array[$key + 2],
                $array[$key + 3]
            ];
        }
    }

}

//we call the function and show its result
var_dump(returnNextThreeLines('1413X', $file));

It is better to set some flag that you found id and some counter to count lines after it to achieve your aim.

<?php
include 'SearchAddrForm.html';

// $file = fopen("addrbook.txt", "a+");
$file = fopen("addrbook.txt", "r");

$status = false;
$data = '';


if (isset($_POST['UserID']))
{
    $iD = $_POST['UserID'];
    $contact = "";

    rewind($file);

    $found = false;
    $count = 1;
    while (($line = fgets($file)) !== FALSE)
    {
        if ($count == 3) // you read lines you needed after you found id
            break;

        if ($found == true)
        {
             $contact .= $line;
             $count++
        }

        if (trim($line) == $iD)
        {
            $found = true;
            $contact = $line;
        }
    }

    echo $contact;
}

fclose($file);
?>

This kind of example how you can achieve this. And as you see in comment you should use $contact .= value, not $contact += value. Also instead of reading you can take the whole file in array line by line using function file . And why are opening file for writing?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM