How do I make this preg_match case insensitive?

PhpRegex

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

Another option:

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGianFSView Question on Stackoverflow
Solution 1 - Phpdonald123View Answer on Stackoverflow
Solution 2 - PhpZomboView Answer on Stackoverflow