How to Make This Preg_Match Case Insensitive

How do I make this preg_match case insensitive?

Just add the i modifier after your delimiter (# in your case):

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

If your delimiter is /, add an i after it :

preg_match("/your_regexp_here/i", $s, $matches); // i means case insensitive

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

Cant make case-insensitive regular expression in PHP

If you want to check case-sensitive you have to remove the i:

$page = "Test";
$page1 = "test";
var_dump(preg_match("/^test/", $page)); // int(0)
var_dump(preg_match("/^test/", $page1)); // int(1)

If you want to check case-insensitive you have to add the i:

$page = "Test";
$page1 = "test";
var_dump(preg_match("/^test/i", $page)); // int(1)
var_dump(preg_match("/^test/i", $page1)); // int(1)

demo: http://ideone.com/Ab9nrs

I tried your code on 3v4l.org and it looks good on the most PHP versions: https://3v4l.org/5UeCu

How do I make this preg_match case insensitive?

Just add the i modifier after your delimiter (# in your case):

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

If your delimiter is /, add an i after it :

preg_match("/your_regexp_here/i", $s, $matches); // i means case insensitive

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

Preg_match case insensitive

Add i after the search

$mot="#".$_POST['nom']."#i";
if (preg_match($mot,$line)){ $existe=true;break 1;}

PHP case insensitive usermame match

You can do a case insensitive match in PHP with i. For instance, the following will print 'This matches!':

<?php
if ( preg_match('/def/i', 'ABCDEF') ) {
echo 'This matches!';
}
?>

So just add i to the pattern, and the case will be ignored.

PHP preg_replace: Case insensitive match with case sensitive replacement

I have in mind this implementation for common case:

$data    = 'this is appLe and ApPle';
$search = 'apple';
$replace = 'pear';

$data = preg_replace_callback('/\b'.$search.'\b/i', function($matches) use ($replace)
{
$i=0;
return join('', array_map(function($char) use ($matches, &$i)
{
return ctype_lower($matches[0][$i++])?strtolower($char):strtoupper($char);
}, str_split($replace)));
}, $data);

//var_dump($data); //"this is peaR and PeAr"

-it's more complicated, of course, but fit original request for any position. If you're looking for only first letter, this could be an overkill (see @Jon's answer then)

Regex - match whole line if has a specific word and case insensitive with PHP preg_match_all

This expression,

(?i)^.*\bworld\b.*$

might simply return those desired strings.

Test

$re = '/(?i)^.*\bworld\b.*$/m';
$str = 'this is my WoRld
this is my world
this is my wORLD
this is my home';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

var_dump($matches);

Output

array(3) {
[0]=>
array(1) {
[0]=>
string(16) "this is my WoRld"
}
[1]=>
array(1) {
[0]=>
string(16) "this is my world"
}
[2]=>
array(1) {
[0]=>
string(16) "this is my wORLD"
}
}

RegEx Circuit

jex.im visualizes regular expressions:

Sample Image



Related Topics



Leave a reply



Submit