php-regexHow to use regex in PHP to do a case insensitive search?
To do a case insensitive search using regex in PHP, you can use the i
modifier. This modifier will make the regex pattern case insensitive.
Example code
$string = 'Hello World';
$pattern = '/hello/i';
if (preg_match($pattern, $string)) {
echo 'Match found';
}
Output example
Match found
Code explanation
$string
: This is the string that we are searching in.$pattern
: This is the regex pattern that we are using to search. Thei
modifier makes it case insensitive.preg_match()
: This is the function used to search for a pattern in a string.
Helpful links
More of Php Regex
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match a zip code?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match UUID?
- How to use PHP regex to match UTF8?
- How to use PHP regex to match whitespace?
- How to remove all non-numeric characters using PHP regex?
See more codes...