0

OK, i have a function that compares values and returns the results, regardless of case, ie: Interfacility Transfer = INTERFACILITY TRANSFER here is the function:

function fncResult ($expVal, $actVal)
{
    $negNulls=array("-5","-10","-15","-20","-25");
    if (!in_array($expVal, $negNulls))
    {
        if(strtolower($expVal)==strtolower($actVal))
        {
            echo "
            <td class='match' title='The values match.'>Match</td>
        </tr>";
        }
        else
        {
            echo "
                            <td class='notMatch'  title='The values do not match.'>Not Match<br />No Match</td>
                        </tr>";
        }
    }
    else
    {
        echo "
            <td class='null'  title='The value in the XML was a negative null.'>Negative Null</td>
        </tr>";
    }
}

It works about 99% of the time except when it comes to this:

//--Type of service requested
        echo "
            <tr>
                <td>E02_04</td>
                <td>Type of Service Requested</td>
                <td>36. &lt;Nature of Call&gt;</td>
                <td>$fldServReq</td>
                <td>".fncGrabNemsis("E02_04",$fldServReq,$local)."</td>
                <td>".fncIsSet($CZ_E02_04[1])."</td>";
        fncResult(fncGrabNemsis("E02_04",$fldServReq,$local),fncIsSet($CZ_E02_04[1]));

Although it looks more complicated, it really is just a strtolower($expVal)==strtolower($actVal), comparison. When I echo the values being compared, I get: "interfacility transfer" and "interfacility transfer" and "No Match"... WTF? Could it be because the first value is coming from a XML (UTF-8) and the second is from a DB (?) I have no idea what to do and am incredibly frustrated since this a simple task. Thanks for any help!

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
jreed121
  • 2,067
  • 4
  • 34
  • 57
  • 4
    Why don't you just use [strcasecmp](http://uk3.php.net/manual/en/function.strcasecmp.php)? – John Parker Feb 07 '11 at 21:10
  • There might be whitespaces. `var_dump` and check if their lengths are equal, or try `trim` on the input first. – netcoder Feb 07 '11 at 21:16
  • 2
    All this code is useless without seeing the data being supplied to it. It seems very likely that you are having trouble with character encoding. – user229044 Feb 07 '11 at 21:17
  • This user may have encountered a similar problem: http://stackoverflow.com/questions/3636559/strange-utf8-string-comparison/3636631#3636631 . Likewise, this: http://stackoverflow.com/questions/1089966/utf8-filenames-in-php-and-different-unicode-encodings/1934029#1934029 . In other words, the same Unicode character can be represented by different sequences of bytes. Can you post a *hex* dump of the strings? – Piskvor left the building Feb 07 '11 at 21:20

2 Answers2

0

Is there any trailing whitespace on your strings? Perhaps nesting a trim() along with a strtolower() would clear that up? If you're looking at this in HTML output, take a look at the source and make sure there's not an HTML entity in there messing it up (i.e. "interfacility transfer" and "interfacility&nbsp;transfer" are not the same, but may look the same rendered in HTML).

The final option is to "upgrade" to the mb_strtolower and see if it is an encoding issue.

MidnightLightning
  • 6,715
  • 5
  • 44
  • 68
0

Print out the bytes of expval and actval (with urlencode, for example). There are a lot of different characters that look exactly the same (for example, normal space and non-breaking space, or c, es and roman 100).

Tgr
  • 27,442
  • 12
  • 81
  • 118