Domanda

I have two strings (which are supposed to be the same). One is pulled from an API result and one is entered by the user. I'm trying to compare them and failing. When I var_dump, I get the following:

var_dump($str1);
var_dump($str2);

string(21) "Software & Technology" 
string(25) "Software & Technology"

Notice the incorrect length of $str2. Anyone know what's going on here?

È stato utile?

Soluzione

It appears that you have HTML ampersand character & in one of the string. You should use html_entity_decode before comparing strings:

if (html_entity_decode($str1) == html_entity_decode($str2)) {
    // ...
}

Live Demo: http://ideone.com/pkWEJC

Altri suggerimenti

Using html_entity_decode will address the ampersand (and other character) issues, and using strcmp will take care of the rest.

if (strcmp(html_entity_decode($str1), html_entity_decode($str2)) == 0) {
    // ..
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top