#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Posts
    573
    Rep Power
    70

    Comparing strings for equality shouldn't be difficult...


    Hi

    I have some strings stored in a serialized array and when I search the unserialized array for matches to new strings, PHP's string comparisons always return false. For example

    if ($string === 'bob')
    // return existing bob record
    else
    // create new bob record

    Problem is, even if $string contains 'bob' it always creates a new record.

    I also tried

    if (strcmp($string, 'bob') == 0)
    // return existing bob record
    else
    // create new bob record

    Same deal. I'm getting dozens of identical 'bob' records because PHP can't figure out thet 'bob' does in fact equal 'bob'.

    Here's the actual code:

    php Code:
     
     
    function cache_exists($inPath)
    {
    	//return null;
    	$encoded	= base64_encode($inPath);
    	echo '---------------------------------------------------------<br>';
    	echo 'starting cache_exists<br>';
    	foreach($this->cache['files'] as $value)
    	{
    		echo 'comparing: '.$value['path'].' to '.$encoded.'<br>';
    		if (strcmp($value['path'], $encoded) === 0)
    		{
    			echo 'match found<br>';
    			return true;
    		}
    	}
     
    	echo 'no match found<br>';
    	echo '---------------------------------------------------------<br>';
    	return false;
    }


    and here's the output from that

    ---------------------------------------------------------
    starting cache_exists
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    no match found
    ---------------------------------------------------------

    Anyway, you get the idea... Anyone have any ideas?

    Thanks for any help
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,547
    Rep Power
    2337
    comparing: Li4vQ29udGVudC9hcnQv to Li4vQ29udGVudC9hcnQ=
    The first is ../Content/art the second is ../Content/art/

    They are different. Or am I misunderstanding?

    Comments on this post

    • ktoz agrees : One of those embarrassing "Doh" moments. Thanks!
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Posts
    573
    Rep Power
    70
    Originally Posted by ptr2void
    The first is ../Content/art the second is ../Content/art/

    They are different. Or am I misunderstanding?
    By gods, you're right! I knew it would end up being something stupid. Turns out I write the cache file using the unmodified url as an identifier, but when I do the comparison, I'm using a modified url with the trailing '/'.

    Thanks!
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    Also, you're using "equals" to look for "contains." That's not right. You've used two different ways of comparing equality and both times you refer to equality as "contains."
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo