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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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