开发者

Case-insensitive statements?

开发者 https://www.devze.com 2023-03-04 20:39 出处:网络
Please forgive the ignorance, but I\'m very new to PHP and I couldn\'t find what I want out there anywhere. So I\'ve com here, where I know I\'ll get an answer.

Please forgive the ignorance, but I'm very new to PHP and I couldn't find what I want out there anywhere. So I've com here, where I know I'll get an answer.

What I want to do is accommodate for any letter case inconsistencies in the data I am pulling in (which I didn't create).

For example I would like the following if statement to work even if the $var is written as 'hello', 'HELLO', 'Hello', 'HeLlo', 'hELLo', 'hElLo',.. etc etc.

if ($var =开发者_高级运维= 'hello') {
 echo 'It works!';
}

Obviously that won't, but what could I do to that to make it not worry about the case of the text?

I have had a look around for these kinds of things, I found strcasecmp(), but to me that doesn't seem to be exactly what I want.


Why don't you try:

if (strtolower($var) == 'hello') {
 echo 'It works!';
}

So if $var = 'HelLo';, it will put it to lower case


Use strtolower

eg:

$var = 'HeLLo';
if (strtolower($var) == 'hello') {
 echo "it works";
}


There's a dedicated PHP function for this - strcasecmp($str1, $str2). However, I would still go with the strtolower($var) because you don't have to worry about unicode.

0

精彩评论

暂无评论...
验证码 换一张
取 消