PHP 不区分大小写的 in_array 函数 [英] PHP case-insensitive in_array function

查看:39
本文介绍了PHP 不区分大小写的 in_array 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用in_array函数时是否可以进行不区分大小写的比较?

Is it possible to do case-insensitive comparison when using the in_array function?

对于这样的源数组:

$a= array(
 'one',
 'two',
 'three',
 'four'
);

以下查找都将返回 true:

The following lookups would all return true:

in_array('one', $a);
in_array('two', $a);
in_array('ONE', $a);
in_array('fOUr', $a);

什么函数或函数集会做同样的事情?我不认为 in_array 本身可以做到这一点.

What function or set of functions would do the same? I don't think in_array itself can do this.

推荐答案

你可以使用 preg_grep():

you can use preg_grep():

$a= array(
 'one',
 'two',
 'three',
 'four'
);

print_r( preg_grep( "/ONe/i" , $a ) );

这篇关于PHP 不区分大小写的 in_array 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆