Disable case sensitive only in a comparison

Asked

Viewed 149 times

1

I wonder if there’s a way "disable" the case sensitive in a certain comparison.

Example:

var_dump('SQL'=='SQL'); # bool(true)

var_dump('SQL'=='sQL'); # bool(false)

I know there are ways to solve one of them with strtoupper() or strtolower(), but I would like to know specifically if you can deactivate directly in a comparison.

  • 1

    It also has the function strcasecmp : var_dump(strcasecmp('SQL', 'sQL')), but I don’t know if this is what I expected to see

  • Resolve! It’s comparative between 2 strings so it fits perfectly!

2 answers

2


Not directly, but can be used a function that performs the insensitive comparison, such as the strcasecmp. As I said, you can also manipulate strings to make the manipulation, using strtoupper and strolower.

You can use the function strcasecmp as follows:

if (strcasecmp("sQl", "SQL") == 0) {
    echo 'São iguais';
}

When the function return is 0 is because the two strings are the same. When the first string is less than the second, the return will be < 0. And when the second string is smaller than the first, the return will be > 0. You can see more about the function in the documentation of PHP.

  • Just note that accented strings do not return the expected result. https://ideone.com/oktpEn

0

The short answer is nay

But as you said, you can pass the two values to strtoupper or strolower and make the comparison, or if you want to use regex you can pass the modifier /i as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.