2

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?

1
  • 1
    Did you consider changing the case of both to Upper and then comparing?.. Commented Feb 15, 2013 at 7:08

5 Answers 5

5

strtolower($string1) == strtolower($string2)

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, but strcasecmp() function is faster. if you want to use it with loops in big data reading. Good luck.
4

strcasecmp ( string $str1 , string $str2 ) ignoring the case, returns 0 if they're equal

Comments

2

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

Is solution need to use regex when we already have strtolower, strcasecmp is available :)
1
strcmp ( strtolower($str1) , strtolower($str2))

Comments

1

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.

similar_text(str1, str2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.