不区分大小写的字符串比较

不区分大小写的字符串比较

本文介绍了不区分大小写的字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个变量,看看它们是否相同,但我希望这个比较不区分大小写。

I would like to compare two variables to see if they are the same, but I want this comparison to be case-insensitive.

例如,这将是区分大小写:

For example, this would be case sensitive:

if($var1 == $var2){
   ...
}

但我希望这个不区分大小写,我该如何处理?

But I want this to be case insensitive, how would I approach this?

推荐答案

这很简单;你只需要打电话给两个变量。

This is fairly simple; you just need to call strtolower() on both variables.

如果您需要处理Unicode或国际字符集,可以使用。

If you need to deal with Unicode or international character sets, you can use mb_strtolower().

请注意其他答案建议使用—该功能不处理多字节字符,因此任何UTF-8字符串的结果都是假的。

Please note that other answers suggest using strcasecmp()—that function does not handle multibyte characters, so results for any UTF-8 string will be bogus.

这篇关于不区分大小写的字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:01