Compare String without Case Sensitivity Check - php

I am trying to compare my string like below.
$recieved = "SR1";
if ($recieved == "SR1" || $recieved == "SR2"|| $recieved == "SR3"|| $recieved == "SR4"){
echo "matching";
}
else{
echo "not matching";
}
This is working fine but I want to ignore case sensitivity checking like below
sr1
Sr1
sR1
I don't have idea how can i achieve this?

So you could achieve that by using strtoupper function, also, I would suggest to use in_array like so:
$recieved = "SR1";
if (in_array(strtoupper($recieved), ['SR1', 'SR2', 'SR3', 'SR4'])) {
echo "matching";
} else {
echo "not matching";
}
I hope that works well for you.

We can try using preg_match in case insensitive mode:
$received = "sR1";
if (preg_match("/sr1/i", $received, $matches)) {
echo "match";
}
This approach would also be a good starting point if you might have the need to match sR1, SR1, etc., as it appears inside a larger string. In that case, we could try searching for \bsr1\b using preg_match in case insensitive mode.

Related

php shorthand for or statement [duplicate]

Basically what I'm wondering if there is a way to shorten something like this:
if ($variable == "one" || $variable == "two" || $variable == "three")
in such a way that the variable can be tested against or compared with multiple values without repeating the variable and operator every time.
For example, something along the lines of this might help:
if ($variable == "one" or "two" or "three")
or anything that results in less typing.
in_array() is what I use
if (in_array($variable, array('one','two','three'))) {
Without the need of constructing an array:
if (strstr('onetwothree', $variable))
//or case-insensitive => stristr
Of course, technically, this will return true if variable is twothr, so adding "delimiters" might be handy:
if (stristr('one/two/three', $variable))//or comma's or somehting else
$variable = 'one';
// ofc you could put the whole list in the in_array()
$list = ['one','two','three'];
if(in_array($variable,$list)){
echo "yep";
} else {
echo "nope";
}
With switch case
switch($variable){
case 'one': case 'two': case 'three':
//do something amazing here
break;
default:
//throw new Exception("You are not worth it");
break;
}
Using preg_grep could be shorter and more flexible than using in_array:
if (preg_grep("/(one|two|three)/i", array($variable))) {
// ...
}
Because the optional i pattern modifier (insensitive) can match both upper and lower case letters.

PHP - if condition match exact value including decimal

i am trying to match exact number with decimal. i have tried following but doesn't work
tried following code, but doesn't work with decimal.
<?php
$number=1.23;
$numbers=1.28;
if (is_float($number)==is_float($numbers))
{
echo 'matched';
}else{
echo 'not matched';
}
?>
please check where i am doing mistake or it's totally wrong way to do that. i have check above in PHP sites.
Use floatval (which returns the float value of the given variable) instead of is_float (which returns true if the given variable is a float and false if it isn’t).
You could simply make use of a strict matching operator === instead of floatval or is_float.
<?php
$number=1.23;
$numbers=1.28;
if($number === $numbers)
{
echo "Matched";
}
else { echo "No Match"; }

In PHP, is there a short way to compare a variable to multiple values?

Basically what I'm wondering if there is a way to shorten something like this:
if ($variable == "one" || $variable == "two" || $variable == "three")
in such a way that the variable can be tested against or compared with multiple values without repeating the variable and operator every time.
For example, something along the lines of this might help:
if ($variable == "one" or "two" or "three")
or anything that results in less typing.
in_array() is what I use
if (in_array($variable, array('one','two','three'))) {
Without the need of constructing an array:
if (strstr('onetwothree', $variable))
//or case-insensitive => stristr
Of course, technically, this will return true if variable is twothr, so adding "delimiters" might be handy:
if (stristr('one/two/three', $variable))//or comma's or somehting else
$variable = 'one';
// ofc you could put the whole list in the in_array()
$list = ['one','two','three'];
if(in_array($variable,$list)){
echo "yep";
} else {
echo "nope";
}
With switch case
switch($variable){
case 'one': case 'two': case 'three':
//do something amazing here
break;
default:
//throw new Exception("You are not worth it");
break;
}
Using preg_grep could be shorter and more flexible than using in_array:
if (preg_grep("/(one|two|three)/i", array($variable))) {
// ...
}
Because the optional i pattern modifier (insensitive) can match both upper and lower case letters.

PHP: Switch weird behavior [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP expresses two different strings to be the same
I have a problem understanding what's causes this weird behavior in a switch case instruction.
The code is this:
<?php
$myKey = "0E9";
switch ($myKey) {
case "0E2":
echo "The F Word";
break;
case "0E9":
echo "This is the G";
break;
default:
echo "Nothing here";
break;
}
?>
The result of this instruction should be This is the G
Well, not so. always returns The F Word
If we reverse the 0E9 left instructions for the beginning and try to find the value 0E2
<?php
$myKey = "0E2";
switch ($myKey) {
case "0E9":
echo "The G String";
break;
case "0E2":
echo "The F Word";
break;
default:
echo "Nothing here";
break;
}
?>
Now returns always This is the G
0E2 and 0E9 values ​​are not interpreted as text? Those Values ​​are reserved?
Someone can explain this behavior?
"0E2" == "0E9" is true because they are numerical strings.
Note: switch use loose comparision.
Check this question: PHP expresses two different strings to be the same.
Numeric strings such as these are equal to each other .. always. Unfortunately, there is no way to force an equivalence comparison via switch. You just have to use if:
if ($myKey === '0E9') {
echo 'g';
}
else if ($myKey === '0E2') {
echo 'f';
}
else {
echo "Nothing here";
}
You could also trim the leading zero, I suppose.

how do I say "If a string contains "x" in PHP?

I have a variable:
$testingAllDay = $event->when[0]->startTime;
This variable will be this format if it is "All Day":
2011-06-30
It will be this format if it is not "All Day":
2011-07-08T12:00:00.000-05:00
I'm wanting to do something like:
if ($testingAllDay does not contain "T"){
$AllDay = 1;
} else {
$AllDay = 0;
}
Do I need to use a strstr() here, or is there another function that does this? Thanks!
One option is to use strpos to see if the 'T' character is present in the string as follows:
if (strpos($testingAllDay, 'T') !== false) {
// 'T' was present in $testingAllDay
}
That said, it would probably be faster/more efficient (although no doubt meaninglessly so) to use strlen in this case, as according to your example, the time-free field will always be 10 characters long.
For example:
if(strlen($testingAllDay) > 10) {
// 'T' was present in $testingAllDay
}
Use strpos:
if (strpos($testingAllDay,"T")!==false){
or strstr
if (!strstr($testingAllDay,"T")){
if (strpos($testingAllDay, 'T') !== FALSE){
...
}
If those are the only possible cases, even strlen() will do.
not exactly answer to the question, but you could check with strlen().
i.e. "All Day" length is 10, anything above that is not.
The function you're looking for is strpos(). The following is an example picking up your wording for the variable names even:
$testingAllDayTPosition = strpos($testingAllDay, 'T');
$testingAllDayDoesNotContainT = false === $testingAllDayTPosition;
if ($testingAllDayDoesNotContainT){
$AllDay = 1;
} else {
$AllDay = 0;
}
strstr and strpos are two functions by which you can complete your requirement.
strstr will see if substring exists in string and it will echo from first occurrence of string to rest.
While strpos will give you position of first occurrence of the string.

Categories