Jump to content

Archived

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

para11ax

String Compares?

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Try:
[code]if(!ereg("^[ ]*$",$line){[/code]

Orio.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote]Ideas on why a space isn't a space?[/quote]

When it's '%20' not ' '

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.