para11ax Posted August 6, 2006 Share Posted August 6, 2006 PHP seems totally incompetant when it comes to comparing strings. I have had many issues with this while writing a program to parse a log file. I have ignored previous problems, but now one is stopping the program from working at all.Basically, there is a line that is just a space (" ")... I had not programmed for this and the parser timed out. After debugging for a long time I figured that out. Here's the problem though. I added code to make sure that the line isn't a space or null... and only parse that line if it is something substancial. BUT, it's not working.[code=php]$line = fgets($log);if(!($line == "") & !($line == " ")){ parse($line, $server, $log, $current_round, $name_guid, $line_num);}[/code]This seems pretty straightforeward... but amazingly utterly fails. The parser still times out... and if I put a stop inside the IF and output the line, like so:[code=php]$line = fgets($log);if(!($line == "") & !($line == " ")){ echo "($line)"; exit(); parse($line, $server, $log, $current_round, $name_guid, $line_num);}[/code]Guess what the output is?[quote="Output"]( )[/quote]A SPACE! What is wrong with PHP when it comes to strings. Just as a note... I've also used === and strcmp to no avail. Nothing seems to be able to compare strings.Any ideas?EDIT: As an added note... PHP doesn't seem to recognize this as a space ever... even though it is obviously a space based on the output. If I use str_replace(" ", "A", $line)... I still get " " as the output.Ideas on why a space isn't a space? Quote Link to comment Share on other sites More sharing options...
Orio Posted August 6, 2006 Share Posted August 6, 2006 It should be:if(!($line == "") && !($line == " ")){With two "&".But I think it's better to use a regular expression here, so if the line contains nothing or a single space or a million white spaces- it will return false any ways.if(!ereg("[ ]*",$line){Orio. Quote Link to comment Share on other sites More sharing options...
para11ax Posted August 6, 2006 Author Share Posted August 6, 2006 Adding the extra & didn't change the behavior at all.I tried the regular expression code you gave... and it did make it to the end of the log... but it didn't parse a single line, so it looks like that expression evaluated to TRUE for anything. Quote Link to comment Share on other sites More sharing options...
Orio Posted August 6, 2006 Share Posted August 6, 2006 Try:[code]if(!ereg("^[ ]*$",$line){[/code]Orio. Quote Link to comment Share on other sites More sharing options...
para11ax Posted August 6, 2006 Author Share Posted August 6, 2006 That brings me back to the original behavior of going into the IF statement, even though it is a space.[code=php]if(!ereg("^[ ]*$",$line)){ echo "($line)"; exit(); parse($line, $server, $log, $current_round, $name_guid, $line_num);}[/code][quote="Output"]( )[/quote] Quote Link to comment Share on other sites More sharing options...
Orio Posted August 6, 2006 Share Posted August 6, 2006 I think that's because every line has in the end either \n or \r\n or \r.Try:[code]<?php$line=str_replace(array("\r","\n"),"",$line);if(!ereg("^[ ]*$",$line)){ echo "($line)"; exit(); parse($line, $server, $log, $current_round, $name_guid, $line_num);}?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
para11ax Posted August 6, 2006 Author Share Posted August 6, 2006 Looks like it's working. Thanks!I wish something that seems so simple would be. I'll let you know if it hangs up anywhere else... it could take a while to run through. Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 6, 2006 Share Posted August 6, 2006 [quote]Ideas on why a space isn't a space?[/quote]When it's '%20' not ' ' Quote Link to comment Share on other sites More sharing options...
para11ax Posted August 6, 2006 Author Share Posted August 6, 2006 [quote author=AndyB link=topic=103209.msg410886#msg410886 date=1154896211][quote]Ideas on why a space isn't a space?[/quote]When it's '%20' not ' '[/quote]True enough, but the way I found the problem was by reading the log in my PHP editor, and the line is blank. So I think it must be that PHP is considering the line breaks and such in the ASCII formated file... since they wouldn't show up anywhere.Thanks for everyone's input. Quote Link to comment 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.