Jump to content


Photo

String Compares?


  • Please log in to reply
8 replies to this topic

#1 para11ax

para11ax
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationColumbus, OH

Posted 06 August 2006 - 07:49 PM

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.

$line = fgets($log);
if(!($line == "") & !($line == " ")){
     parse($line, $server, $log, $current_round, $name_guid, $line_num);
}

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:

$line = fgets($log);
if(!($line == "") & !($line == " ")){
     echo "($line)";
     exit();
     parse($line, $server, $log, $current_round, $name_guid, $line_num);
}

Guess what the output is?

( )


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?

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 06 August 2006 - 07:58 PM

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.
Think you're smarty?

(Gone until 20 to November)

#3 para11ax

para11ax
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationColumbus, OH

Posted 06 August 2006 - 08:06 PM

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.

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 06 August 2006 - 08:08 PM

Try:
if(!ereg("^[ ]*$",$line){

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 para11ax

para11ax
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationColumbus, OH

Posted 06 August 2006 - 08:22 PM

That brings me back to the original behavior of going into the IF statement, even though it is a space.

if(!ereg("^[ ]*$",$line)){
     echo "($line)";
     exit();
     parse($line, $server, $log, $current_round, $name_guid, $line_num);
}

( )



#6 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 06 August 2006 - 08:27 PM

I think that's because every line has in the end either \n or \r\n or \r.

Try:
<?php
$line=str_replace(array("\r","\n"),"",$line);
if(!ereg("^[ ]*$",$line)){
     echo "($line)";
     exit();
     parse($line, $server, $log, $current_round, $name_guid, $line_num);
}
?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#7 para11ax

para11ax
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationColumbus, OH

Posted 06 August 2006 - 08:29 PM

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.

#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 06 August 2006 - 08:30 PM

Ideas on why a space isn't a space?


When it's '%20' not ' '
Legend has it that reading the manual never killed anyone.
My site

#9 para11ax

para11ax
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationColumbus, OH

Posted 06 August 2006 - 08:32 PM

Ideas on why a space isn't a space?


When it's '%20' not ' '


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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users