0

I'm trying to write a code that compares the values from a text document with a value that has been posted from a form.

So far i've got this, but i'm absolutely positive i'm doing something way off.

Thanks in advance for your help!

<form method="POST" action="file.php"> 
<p>
<br /><input type="text" name="name" ><br />
</p>
<p><input type="submit" value="Check" /></p>
</form>

<?php
if (isset($_POST['name'])) {
    $name = $_POST['name'];

    /*The text document contains these written names: 
    Michael 
    Terry 
    John 
    Phillip*/ 

    $lines = file('names.txt'); 

    $names_array = ($lines);   

        if (in_array($name, $names_array)) {
            echo "exists";
        } else {
            echo 'none';
        }
}
?>

UPDATE: Fixed and now working fine!

3 Answers 3

2

The problem is with your file('names.txt') function. Although this returns an array with each line in a seperate key, it also includes the newline charactor on that same line.

So your array actually contains:

$lines[0] = "Michael\n";
$lines[1] = "Terry\n";
$lines[2] = "John\n";
$lines[3] = "Phillip\n";

To prevent this from happening, use file('names.txt', FILE_IGNORE_NEW_LINES)

$lines[0] = "Michael";
$lines[1] = "Terry";
$lines[2] = "John";
$lines[3] = "Phillip";

Now your name should match.

Besides that, why do you use the following?

$lines = file('names.txt'); 
$names_array = ($lines);

//simply use the following.
$names_array = file('names.txt', FILE_IGNORE_NEW_LINES); 
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, just realised that i was making more work for myself there! Fixed it up! Thanks!
0

Read the doc about file : http://www.php.net/manual/en/function.file.php

Note:

Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used, so you still need to use rtrim() if you do not want the line ending present.

Comments

-1



/*The text document contains these written names: 
Michael 
Terry 
John 
Phillip*/ 

$lines = file('data.txt'); //Lets say we got an array with these values   //$lines =array('Michael','John','Terry','Phillip');    $i=0;
foreach($lines as $line)   {
$lines[$i] =trim($line);
$i++;   }    

    if (in_array($name, $lines)) {
        echo "exists";
    } else {
        echo 'none';
    } } ?

Blockquote

data.txt

Michael Terry John Phillip

data.txt has spaces ,so we use trim() to remove it.

11 Comments

Thanks for your help, now everything is working exactly the way i need it to! Marked as answer!
Thats just a terrible solution. First of all its not needed to loop through all the lines. Then to solve it you add the trimmed version to the end of the array, making it twice the length. So you end up with all names double in the list, once with and once without newline char. And you also add a random $i you are increasing for no reason at all.
@HugoDelsing - I'm really new to PHP, so if the answer above is bloated with unnecessary code, thanks for pointing it out! I tried your advice in adding 'ignore new lines' however it still didn't work for me. ie outputting none instead of exists. Any other ideas?
array(4) { [0]=> string(7) "Michael" [1]=> string(5) "Terry" [2]=> string(4) "John" [3]=> string(7) "Phillip" }
Try it yourself Niladri: $lines = array("test", "test2");foreach($lines as $line) { $lines[] = $line;}var_dump($lines); and you'll notice that you will have 4 keys in the final dump.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.