saiyen2002 Posted February 7, 2010 Share Posted February 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/191211-output-of-file-looks-the-same-but-different-comparison-operator-returns-false/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 7, 2010 Share Posted February 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191211-output-of-file-looks-the-same-but-different-comparison-operator-returns-false/#findComment-1008154 Share on other sites More sharing options...
khr2003 Posted February 7, 2010 Share Posted February 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/191211-output-of-file-looks-the-same-but-different-comparison-operator-returns-false/#findComment-1008156 Share on other sites More sharing options...
saiyen2002 Posted February 7, 2010 Author Share Posted February 7, 2010 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] Quote Link to comment https://forums.phpfreaks.com/topic/191211-output-of-file-looks-the-same-but-different-comparison-operator-returns-false/#findComment-1008167 Share on other sites More sharing options...
salathe Posted February 7, 2010 Share Posted February 7, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/191211-output-of-file-looks-the-same-but-different-comparison-operator-returns-false/#findComment-1008319 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.