Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make this preg_match case insensitive?

Tags:

regex

php

Consider:

preg_match("#(.{100}$keywords.{100})#", strip_tags($description), $matches); 

I'm trying to show only 100 characters in each side with the search string in the middle.

This code actually works, but it is a case sensitive. How do I make it case insensitive?

like image 989
GianFS Avatar asked Sep 13 '12 16:09

GianFS


People also ask

Are regex expressions case sensitive?

By default, the comparison of an input string with any literal characters in a regular expression pattern is case-sensitive, white space in a regular expression pattern is interpreted as literal white-space characters, and capturing groups in a regular expression are named implicitly as well as explicitly.

What is the use of Preg_match () method?

The preg_match() function returns whether a match was found in a string.

What is the purpose of Preg_match () regular expression in PHP?

The preg_match() function will tell you whether a string contains matches of a pattern.


2 Answers

Just add the i modifier after your delimiter #:

preg_match("#(.{100}$keywords.{100})#i", strip_tags($description), $matches); 

If the i modifier is set, letters in the pattern match both upper and lower case letters.

like image 129
donald123 Avatar answered Oct 09 '22 13:10

donald123


Another option:

<?php $n = preg_match('/(?i)we/', 'Wednesday'); echo $n; 

https://php.net/regexp.reference.internal-options

like image 35
Zombo Avatar answered Oct 09 '22 11:10

Zombo