Frage

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?

War es hilfreich?

Lösung

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

Andere Tipps

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) {
    // ..
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top