0

I know this is probably one of those "woods for the trees" questions but I've been sat here all day and I just cant see where Im going wrong.

The Scenario:

Basically I have a query returning the following assoc array of address results like so:

Array ( [0] => Array ( [Address_ID] => 12 [Address_String] => 23 Abledone road Hobs moat Solihull West Midlands B92 4JJ ) [1] => Array ( [Address_ID] => 13 [Address_String] => 22 Holdean Road Deepcut Surrey KT164NJ ) [2] => Array ( [Address_ID] => 14 [Address_String] => 32 Palbrough Road Nottingham N32 4NJ ) [3] => Array ( [Address_ID] => 15 [Address_String] => Flat 2B Gateway House Rutland Street Bearwood Westmidlands B66 9RT ) [4] => Array ( [Address_ID] => 16 [Address_String] => 258 Lincoln Street Bullworth Street Dundee DD35 9RJ ) [5] => Array ( [Address_ID] => 17 [Address_String] => 4 Scott Arms Avenue Beckton Hartfordshire H32 7JJ ) [6] => Array ( [Address_ID] => 18 [Address_String] => 14 Fairfield house Millbank road Bearwood Birmingham Westmidlands B137JJ ) [7] => Array ( [Address_ID] => 19 [Address_String] => 9 Scott road Olton Solihull Westmidlands B91 7TY ) [8] => Array ( [Address_ID] => 20 [Address_String] => Normandy House Bunnian Place Basingstoke RG21 7EJ ) [9] => Array ( [Address_ID] => 27 [Address_String] => 3 New Square Bedfont Lakes Feltham Middlesex TW14 8HB ) [10] => Array ( [Address_ID] => 28 [Address_String] => No. 1 The Square Temple Quay Bristol BS1 6DG ) [11] => Array ( [Address_ID] => 29 [Address_String] => The Visual Space Capital Park Fulbourn Cambridge CB21 5XH ) [12] => Array ( [Address_ID] => 40 [Address_String] => 21 St. Andrew Square Edinburgh Scotland CB21 5XH ) ) 

And all I'm trying to do is match these results against a search string to see if the address is already in my db.

my code is as follows:

  public function addressExistsByString ($searchAddrString, $asJSON = FALSE) {
    if (!isset($searchAddrString)) {
        throw new Exception('The searchAddrString variable of function ' . __FUNCTION__ . ' of class ' . __CLASS__ . 'was empty or set to NULL.');
    } elseif (!is_string($searchAddrString)) {
        throw new Exception('The type of value for the argument addressID of function ' . __FUNCTION__ . ' of class ' . __CLASS__ . ' was ' . gettype($searchAddrString) . ' a string was expected');
    }if (!is_bool($asJSON)) {
        $asJSON = FALSE;
    }

   print_r ($addressArray = $this->getAllLocations(FALSE));

    if ($addressArray === -1) {
        return -2;
    } else {

        $search = str_replace(',','',  strtolower(trim($searchAddrString)));

        foreach ($addressArray as $record) {

           $recID = $record ['Address_ID']; 
           $currentAddress =str_replace(',','',  strtolower(trim($record['Address_String'])));

           echo 'Search address: '. $search. ' Current address: '.  $currentAddress .' type of current address: '.gettype($searchAddrString).'<br/>';  

           if ($currentAddress === $search) {

               if ($asJSON) {
                   echo json_encode($recID);
               }
               return intval($recID);
           }
        }
        if ($asJSON) {
            echo json_encode(-1);
        }
        return -1;
    }
}

When I run this code I get the following output (sorry its a little long but the important bit is highlighted ):

bunnian place basingstoke rg21 7ej Current address: 23 abledone road hobs moat solihull west midlands b92 4jj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 22 holdean road deepcut surrey kt164nj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 32 palbrough road nottingham n32 4nj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: flat 2b gateway house rutland street bearwood westmidlands b66 9rt type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 258 lincoln street bullworth street dundee dd35 9rj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 4 scott arms avenue beckton hartfordshire h32 7jj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 14 fairfield house millbank road bearwood birmingham westmidlands b137jj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 9 scott road olton solihull westmidlands b91 7ty type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: normandy house bunnian place basingstoke rg21 7ej type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 3 new square bedfont lakes feltham middlesex tw14 8hb type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: no. 1 the square temple quay bristol bs1 6dg type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: the visual space capital park fulbourn cambridge cb21 5xh type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 21 st. andrew square edinburgh scotland cb21 5xh type of current address: string -1

The strings highlighted are exactly the same, I've tested them by taking them both and sticking them in a separate if statement as plain strings and true is always returned . I've tried ==, === casting, string values and everything else I can think of but for some reason -1 is always returned. I know its probably something very simple Im missing but I just cant see it, can anyone help?

Thanks in advance.

  • 1
    check the str-lengths or put it in a debugger or log within `[]` to check extra spaces around. – जलजनक Feb 03 '13 at 15:48
  • 1
    Can't you isolate the important bit and put it into its own complete program to demonstrate your question? The vast majority of your question has nothing to do with the question. – Lightness Races in Orbit Feb 03 '13 at 15:51
  • 1) `var_dump` the important stuff for us 2) why are you posting so many unnecessary things, including information that you aren't legally allowed to publish (they are under the data protection act, as they are UK addresses). 3) Check for any line-endings or unprintable characters you may have missed – Amelia Feb 03 '13 at 16:04
  • Sorry, their was an extra white space in some of the addresses just before the post code, I had tried the pre formatted tags and just not seen it D'oh. – user1555360 Feb 03 '13 at 16:49
  • Oh and with regard to point 2. made by Hiroto all the addresses are either those of companies and freely available via their web sites or ones I made up for testing. – user1555360 Feb 03 '13 at 16:53
  • i had same issue with comparing strings. It took me about two hours, but in my case trim() solved the problem. – bramish Aug 09 '18 at 18:22

1 Answers1

1

var_dump() your two variables just before you compare them. Echo a <pre> tag before this to see all the whitespace in your browser without the need to switch to the HTML source code.

You most likely miss some whitespace character there.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • I think Sven has the key answer: I'm having this exact same problem, and I did a var_dump, and to my amazement I discovered that I was comparing two objects, and not two strings as I believed. Please see this post: http://stackoverflow.com/questions/2867575/get-value-from-simplexmlelement-object I understand that you are not using XML, but you are using JSON mixed in there. Also, I see that you're using triple equals, when means it will return true if they are equal AND THEY ARE THE SAME TYPE. Be careful if one variable is a string, and the other is something else. – Annatar Oct 15 '13 at 06:36