Search code examples
phpregexfilecommentsnewline

Regex to find results but ignore the commented # line



I'm trying to use regex in PHP to find something in .ipmconf file.
And the data inside the file is categorized in each [V //something] section. This is my .ipmconf file looks like:

    [V5 sentry tuning]
    LossThreshold = some data here
    TopHostApplication = some data here
    #rt_comp_level : compression type allowed for "Real Time" flows
    rt_comp_level = some data here
    #tr_comp_level : compression type allowed for "Transactionnal" flows
    tr_comp_level = some data here
    #bg_comp_level : compression type allowed for "background" flows
    bg_comp_level = some data here
    tunnelcomport = some data here
    
    [V6 something]
    //other data here


Currently my code just able to read the data until the "TopHostApplication = some data here" line, and it stops there. So many data below that are missing, starting from the first comment.
How to ignore the #comments line and continue search another line until it arrive at last line?

here is some part my code:

                $filename = $_FILES['form']['name']['config'];
                $conf = file("./components/com_rsform/uploads/config".$filename);
                $buf = implode("\n",$conf);
                preg_match('/domain = (.*)/m',$buf,$match);
                $domain = $match[1];
                unlink("./com_rsform/uploads/config".$filename);
        
                while(!empty($conf)){
                   set_time_limit(240);
                   $line = array_shift ($conf);
                   if(preg_match('/^\[V\d+ (.*)\]/',$line,$match)){$section = $match[1];}
                      
                   if ($section == "sentry tuning"){
                      $data1 ="";$data2 ="";
                      while (preg_match('/^(.*) = (.*)$/',$line,$matches)){
                      $data1 = $matches[1]; 
                      $data2 = $matches[2]; 
                      $line = array_shift($conf);}
                      $section = "";
                   }
                   elseif($section == "[V6 something]")//next section
                   {//codes
                   }
                }
                //echo $data1 and $data2 somewhere here

I'm new with regex and PHP stuff. Thank you in advance!


Solution

  • I modified your regex to capture the complete section name. The following should process the entire configuration. I have added some echo statements:

    while (!empty($conf)) {
        $line = array_shift($conf);
        if (!preg_match('/^\[(V\d+ .*)\]/',$line,$match)) { // slightly modified regex to get the complete section name
            continue;
        }
        $section = $match[1];
        echo "section = $section\n";
        if ($section == "V5 sentry tuning") {
            while (!empty($conf)) { // processes lines up until the next section
                $line = array_shift($conf);
                if (preg_match('/^\[(V\d+ .*)\]/',$line,$match)) { // start of a new section
                    array_unshift($conf, $line); // put the line back to be processed by the next section
                    break;
                }
                if (substr($line, 0, 1) == '#') { // a comment
                    continue;
                }
                if (preg_match('/^(.*) = (.*)$/', $line, $matches)) {
                    $data1 = $matches[1];
                    $data2 = $matches[2];
                    echo "data1 = $data1, data2 = $data2\n";
                }
            }
        }
        elseif ($section == 'V6 something') {
            // do something here
        }
    }
    echo "End of processing\n";
    

    Prints:

    section = V5 sentry tuning
    data1 = LossThreshold, data2 = some data here
    data1 = TopHostApplication, data2 = some data here
    data1 = rt_comp_level, data2 = some data here
    data1 = tr_comp_level, data2 = some data here
    data1 = bg_comp_level, data2 = some data here
    data1 = tunnelcomport, data2 = some data here
    section = V6 something
    End of processing