Jump to content

[SOLVED] Searching a text file for a string


zaneosak

Recommended Posts

Hello, I am trying to write some php code that searches a text file for a string. The webpage is a registration form and has a drop down of 5 times and a text box to input the users name. The time and name get posted to the php and I am trying to write some code that searches the text file for the time or person's name and gives a error message that the time or person is taken. In the code below I only have the time check which doesn't work but I get no errors and the file writes fine, the check just doesnt run. The text file writes like this "Time , Name" and starts a new line every write.

 

I think I am missing something simple, as I thought this project would be quite easy. Any hints, tips or advice would be appreciated

<?
$time=$_POST["time"];
$person=$_POST["user"];

$file = fopen("register.txt", "a+");
$newperson = "$time , $person\n";

if (strstr($file, $time))
{
echo "The timeslot is taken";
}

else	

fwrite($file, $newperson);
echo "Time: $time";
echo "<br> Name: $person";


fclose($file);
?>

Link to comment
Share on other sites

I dont think this line is correct:

 

strstr($file, $time)

 

The first argument of strstr is a string, but you're giving it a file handle. To read a file in as a string you need to use file_get_contents().

 

<?php
$time=$_POST["time"];
$person=$_POST["user"];

$file = fopen("register.txt", "a+");
$newperson = "$time , $person\n";

if (strstr(file_get_contents($file), $time)) {
    echo "The timeslot is taken";
} else  {
    fwrite($file, $newperson);
    echo "Time: $time";
    echo "<br> Name: $person";
}

fclose($file);
?>

Link to comment
Share on other sites

I would use www.php.net/file to read the file, this will read the file putting each new line into an array.

 

You can then search the array using the www.php.net/foreach function.

 

<?php
<?
$time=$_POST["time"];
$person=$_POST["user"];

$fileCheck = file("register.txt");

$file = fopen("register.txt", "a+");
$newperson = "$time , $person\n";

$infile = false;
foreach ($fileCheck as $line) {
    if (stristr($line, $time) !== false) {
        echo "The timeslot is taken";
        $infile = true;
    }

    if (stristr($line, $name) !== false) {
        echo "The name is taken";
        $infile = true;
    }
}    

if (!$infile) {
    fwrite($file, $newperson);
}

echo "Time: $time";
echo "<br> Name: $person";

?>

 

Not sure if that is the best way, but it would work.

Link to comment
Share on other sites

I dont think this line is correct:

 

strstr($file, $time)

 

The first argument of strstr is a string, but you're giving it a file handle. To read a file in as a string you need to use file_get_contents().

 

<?php
$time=$_POST["time"];
$person=$_POST["user"];

$file = fopen("register.txt", "a+");
$newperson = "$time , $person\n";

if (strstr(file_get_contents($file), $time)) {
    echo "The timeslot is taken";
} else  {
    fwrite($file, $newperson);
    echo "Time: $time";
    echo "<br> Name: $person";
}

fclose($file);
?>

 

I tried this and It gave me a warning about file_get_contents wanting its first parameter to be a string.

 

 

I tried it the way premiso suggested and it seems to work if the timeslot is taken but repeats itself a few times "The time slot is takenThe time slot is taken" and still echo's the name. but if the text file is empty (first entry) it gives me 2 warnings:

 

Warning: file(register.txt): failed to open stream: No such file or directory in /project.php on line 12

Warning: Invalid argument supplied for foreach() in project.php on line 18

 

Link to comment
Share on other sites

<?php
$time=$_POST["time"];
$person=$_POST["user"];

if (file_exists("register.txt")) {
    $fileCheck = file("register.txt");
}else {
    $fileCheck = array(); 
}

$file = fopen("register.txt", "a+");
$newperson = "$time , $person\n";

$infile = false;
foreach ($fileCheck as $line) {
    if (stristr($line, $time) !== false) {
        echo "The timeslot is taken";
        $infile = true;
    }

    if (stristr($line, $name) !== false) {
        echo "The name is taken";
        $infile = true;
    }
}    

if (!$infile) {
    fwrite($file, $newperson);
}

echo "Time: $time";
echo "<br> Name: $person";

?>

 

The above is modified so it will not give out any errors if it is the first entry/has not been created yet.

 

 

Edit: Just missed a question.

 

The question is if name OR time is found do you want to exit and say that the new stuff cannot be added? If so this version will do just that.

 

<?php
$time=$_POST["time"];
$person=$_POST["user"];

if (file_exists("register.txt")) {
    $fileCheck = file("register.txt");
}else {
    $fileCheck = array(); 
}

$file = fopen("register.txt", "a+");
$newperson = "$time , $person\n";

$infile = false;
foreach ($fileCheck as $line) {
    if (stristr($line, $time) !== false) {
        echo "The timeslot is taken";
        $infile = true;
        break;
    }elseif (stristr($line, $name) !== false) {
        echo "The name is taken";
        $infile = true;
        break;
    }
}    

if (!$infile) {
    fwrite($file, $newperson);
}

echo "Time: $time";
echo "<br> Name: $person";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.