Jump to content

String Compares?


para11ax

Recommended Posts

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?
Link to comment
https://forums.phpfreaks.com/topic/16735-string-compares/
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/16735-string-compares/#findComment-70370
Share on other sites

[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.
Link to comment
https://forums.phpfreaks.com/topic/16735-string-compares/#findComment-70377
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.