Preg_match find regex with or without first capital letter

In my project I need to find a special regex that works if the first letter is uppercase or lowercase. Below are examples of strings:

user = username
User = username

      

Now I tried this regex:

'/[^\n]user[^\n]*/'

      

But if the first letter is the capital, this expression finds nothing, so my question is:

What would be the correct regex to find the string containing "user" in it in both cases?

+3


source to share


2 answers


You can use the following regex:

$re = "/^user.*$/mi"; 

      



See demo

i

option means "ignore case", and m

means "multiline", forcing ^

and $

matching line boundaries. .

will match any character but a newline (since single line mode is not enabled).

+7


source


in regex to ignore the case we have to use i so you can use the following code '/[^\n]user[^\n]*/i'



0


source







All Articles