Search code examples
phpregexpreg-match

preg_match lets an unwanted line slip through


I am using preg_match to put conditions on the lines, to process, within a loop. Here is my code:

$import = 'LT_VALUES2';
$contents = explode('-',$import,2);
$line = 'DATA LT_VALUES2 TYPE TABLE OF RGSB4 WITH HEADER LINE,';
if(preg_match('/(DATA $import)/i',$line) == 0 && preg_match("/($import\b|$contents[0]\W)/i",$line) == 1 && preg_match("/[']/",$import) == 0 && preg_match("/(\D)/",$import) == 1)
{

It reaches here for no reason

}

why it is failing to process the conditions I provided? I tested the same code at phptester.net and it works the way I want


Solution

  • Single quoted strings will not interpolate variables, which is probably what you're expecting it to do.

    '/(DATA $import)/i' is literally /(DATA $import)/i.

    On the other hand;

    "/(DATA $import)/i" would interpolate to /(DATA LT_VALUES2)/i