0

I have one Array which contain two elements, i am taking input from the user from the API request parameters sometimes it contains capital and small letters ,based on that i am writing some conditions ,Now what my requirement is if user enters small letters also it should match the given array please help me to fix the issue..

$array=['Admin','Root'];

if(in_array($request->role,$array)){
if($request->role== 'Admin'){
     //my logic
  }
}

URL-1:-
in this scenario it passes the condition

/v1/Users?role=Admin

URL-2:- /v1/Users?role=admin

this scenario fails , i need to pass this scenario also

0

2 Answers 2

0

you can convert the string using strtolower() php function to convert string to lower case and then match

$array=['admin','root'];
$role = strtolower($request->role);

if ( in_array($role,$array) ) {
   if( $role == 'admin') {
     //my logic
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you only have 1 word roles like in your example, and you only need to match your specified case and all lowercase, apply ucfirst() on the role.

$array=['Admin','Root'];
$role = ucfirst($request->role);

if(in_array($role,$array)){
if($role== 'Admin'){
     //my logic
  }
}

This will match Admin and admin but not AdMiN or any other casing.

Comments