Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to assume that your regex's are correct. You're actually appending everything to the same string using <code>$commands .= &lt;some text&gt;</code>. Rather than appending it to the same string, you should make <code>$commands</code> an array. Then you want to instantiate a new array for every line you go into (<code>$line</code>). <code>push</code> the things you want to keep onto your <code>$line</code> array, and at the end of every line you <code>array_push($commands, $line)</code></p> <p><code>Array_push()</code>: <a href="http://php.net/manual/en/function.array-push.php" rel="nofollow">http://php.net/manual/en/function.array-push.php</a></p> <p>this could be an approach for your <code>foreach()</code></p> <pre><code>foreach ($lines as $line) { $commands = new array; if (empty($line)) { continue; //empty lines are silly } // ... do all the checks to validate if you currently have a valid LINE //We know that whatever we have now is a valid line $segments = explode(line bla bla); //explode this line on tabs or spaces .. dunno what the output of your console is. //At this point you know exactly where every part you need is, considering the console always gives the same output. //Either you do some validations here, or you go for the lazy approach and go: $newEntry = array( 'Number' =&gt; $segments[0], 'Chain' =&gt; $segments[1], 'Protocol' =&gt; $segments[2], 'Port' =&gt; $segments[3], 'in' =&gt; $segments[4], 'out' =&gt; $segments[5] ); array_push($commands, $newentry); } </code></pre> <p>code is not tested, but you should get an idea where i'm going to.</p> <p>Your code: </p> <pre><code>//instantiate a new empty named variable. $commands = ''; //for every line in your console-dump file foreach ($lines as $line) { //Is the line empty if (empty($line)) { //skip line and go to next line continue; } //Does the line start with # if (preg_match('/^#/', $line) === 1) { //skip line and go to next line continue; } //Does the line start with * if (preg_match('/^\*/', $line) === 1) { //skip line and go to next line continue; } //Does the line start with 'COMMIT' if (preg_match('/^COMMIT/', $line) === 1) { //skip line and go to next line continue; } //we have a valid line now, so let's do stuff $match = array(); //Does the line contain: if (preg_match('/^:([A-Z ]*).*/', $line, $match) === 1) { //skip line and go to next line continue; } //Only if the line is a 'valid' line, and does not have your last match, //Then you will append "iptables {$line}\n" to $commands $commands .= "iptables {$line}\n"; //This way it was a bit useless for you to cut up the line using the preg_match //and store it into $match, cause you don't do anything with $match } </code></pre> <p>Your initial approach is good. And depending how your console returns predictable values, you either follow your approach with the regex (but then actually use the result) or you just go the lazy approach and use the one i suggested.</p> <p>Do note that <code>CONTINUE</code> actually breaks your current loop, and goes into the next iteration.</p> <pre><code>$array = array(1, 2, 3, 4, 5); foreach($array as $number){ if($number &lt; 3){ continue; } print($number .'\n'); } </code></pre> <p>The outcome will be: </p> <pre><code>3\n 4\n 5\n </code></pre> <p>as continue actually completely stops this attempt in your <code>foreach()</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload