Jump to content

output of file() looks the same but different, comparison operator returns false


Recommended Posts

I am reading a file and outputting the contents of the file line by line to the browser. On every line i am checking to see if a certain string is present with the == operator, if it is, then a line will be printed out. the thing is, i know when the == should be true, but it still comes up as false. I decided to a sha1 check between the strings and it seems that they are indeed differnet, but i dont understand how. below is my code

 

$lines = file("template.txt");

 

foreach ($lines as $line_num => $line) {

    echo "Line #<b>$line_num</b> : " .$line. "<br />\n"; // print out line one by one

        $ops = "change";                                                      // string to check for

        echo sha1($ops).' sha1 of $ops<br />';                    // sha1 of string to check for

        echo sha1($line).' sha1 of $line<br />';                    // sha1 of line retrieved from file

        if ($line == $ops )                                                      // check to see if both are the same

        echo "new file needed <br />";

        else

        echo "new file not needed <br />";

}

 

here is an extract of the relevant out put

 

Line #20 : change

7550b672e162c224c309bdea5d48ca975081a904 sha1 of $ops

e0392f84097cc4c072c1d0f37422eeb20154d721 sha1 of $line

new file not needed

 

As you can see the sha1 are different. I need to be told how they are different when the output to the screen is the same as $ops. $ops is something that i copied and pasted out of the file i am outputting!!

 

thanks

By default, file() includes any new-lines that are in the file. You might also have tabs or spaces as part of the data in the file. You should probably use trim() on the string before using it.

I think it is hard to determine the difference without knowing the contents of the file templates.txt. My guess is that there is a space or some other letters that cange the value of Sha1.

 

Since your are looking for a string within a text why don't you use strstr function. This operator "==" requires both sides of the equation to be exactly the same while strstr function searches a whole text for one word.

 

check it out here http://php.net/manual/en/function.strstr.php

thanks guys, i got it working with both strstr() and trim(). This obstacle has been overcome, however it is till nagging me what the difference was.

 

is there any way of finding out what it was that caused the problem? I attached the file if it helps

 

[attachment deleted by admin]

As has already been mentioned in this thread, the values within the $lines array will have line endings attached. In your case, each line has a trailing \n making the value on line 20 "change\n".  Your comparison value, $ops, is simply "change" so the two values are not equal.

 

Rather than using trim or another string manipulation approach, the thing to do would be to tell file not to include those line endings at all. That can be done by providing a behaviour flag to the call to file like:

$lines = file("template.txt", FILE_IGNORE_NEW_LINES);

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.