We help IT Professionals succeed at work.

preg_match() [function.preg-match]: Unknown modifier 'd'

gautam_reddyc
on
Hello has anybody ever received this warning?? everything seems to be ok, and is working fine but I still receive this warning. The hilarious thing is, the warning appears even when 'd' isn't a character in the data being passed. I have tried to google it but, not a lotof help.
foreach($brk as $key=>$value)
{
$pattern1 = "/^option/";	
$pattern2 = "/^".$brk['rowId']."selectoption_/";
							if(preg_match($pattern1,$key) || preg_match($pattern2,$key))
{
//code//
}
}

Open in new window

Comment
Watch Question

ioboxWeb Architect

Commented:
can you post an array with content of $brk?

Author

Commented:
I have attached an array which would go through the regular expressions for preg_match.
Array
(
[0]=>
Array(
    [rowId] => cats/dogs_4
    [type] => select
    [question] => Q4
    [checkBox] => 0
    [SBID] => cats/dogs_4select
    [option0] => o1
    [option1] => o2
    [option2] => o3
    [OEMCmntstextArea] => 
    [IntlSpnttextArea] => 
    [ExtlSpnttextArea] => 
)
[1]=>
Array
(
    [rowId] => cats/dogs_5
    [type] => select
    [question] => Q5
    [checkBox] => 1
    [SBID] => cats/dogs_5select
    [option0] => o4
    [option1] => o5
    [option2] => o6
    [OEMCmntstextArea] => 
    [IntlSpnttextArea] => 
    [ExtlSpnttextArea] => 
)
)

Open in new window

Author

Commented:
I match the keys which would have the string option and put them into a different array, as they go into a different table, while the rest go into my main table.
Web Architect
Commented:
1. you should escape cats/dogs_5 with cats\/dogs_5 (untested)

2. when you do the preg_match $value is an array so you should do 1 more foreach
something like

foreach($sbk as $key=>$value){

   foreach($value as $k=>$v){
     $v = str_replace('/d','\/d',$v);
   }


}

Most Valuable Expert 2011
Top Expert 2015
Commented:
>>  1. you should escape cats/dogs_5 with cats\/dogs_5

I agree. You need to escape the forward slashes you put in your patterns; or you can change the delimiters of your patterns. One example:
$pattern1 = "#^option#";
$pattern2 = "#^".$brk['rowId']."selectoption_#";

Open in new window

Hi,

Try this code.

foreach($brk as $key=>$value)
{
$pattern1 = "/^option/";      
$pattern2 = "/^".$brk['rowId']."selectoption_/";
                                          if(preg_match($pattern1,$value|| preg_match($pattern2,$value))
{
//code//
}
}



Thanks
Gary
Terry WoodsIT Guru
Most Valuable Expert 2011

Commented:
If you can have characters in your pattern that having special meaning to regular expressions, and you don't want them to have special meaning, then you should use the preg_quote function, like this (then you don't need to alter your data in the db):
$pattern2 = "/^".preg_quote($brk['rowId'],"/")."selectoption_/";

Open in new window

Author

Commented:
Thanks!