I need to check if $string1 == $string2 but I dont want it to matter if ones uppercase and the other is lowercase, for example I would like if (harrisburg == HARRISBURG) to return true! What's the best way to do this?
-
1Did you consider changing the case of both to Upper and then comparing?..venkatKA– venkatKA2013-02-15 07:08:31 +00:00Commented Feb 15, 2013 at 7:08
5 Answers
strtolower($string1) == strtolower($string2)
1 Comment
Try this,
$str1 = 'harrisburg';
$str2 = 'HARRISBURG';
Option-1: If $str2 value is uppercase.
strtolower('$str2');
for more details strtolower — Make a string lowercase
Option-2: Case-insensitive string comparision
strcasecmp ($str1 ,$str2)
for maore details strcasecmp — Binary safe case-insensitive string comparison
Note(for option-2): Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
may this help you.
1 Comment
Apparently there is a function for this in PHP? This function will return the value of similarity between the two strings passed. Not exactly what the OP was asking for but thought would be a relevant answer considering the question title.