Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ampersand in String causing incorrect length/comparison

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?

like image 767
user1032752 Avatar asked Apr 09 '26 01:04

user1032752


1 Answers

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

like image 100
anubhava Avatar answered Apr 11 '26 15:04

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!