title: Loop inside loop only running once
body: <p>For educational purposes I'm trying to build a <a href="http://jade-lang.com/" rel="nofollow">PUG</a> Parser. 
  I have two loops, one loops the lines of the PUG-Code, the other loops unclosed tags I saved last iteration.</p>
  
  <p><strong>In my code it seems like the inner loop is only run once, because when I put <code>echo</code> statements for debugging I'm not getting enough output.</strong></p>
  
  <pre><code>foreach ($lines as $line) {
      #Increment the line number
      $lineNum++;
  
      #Save the current indentation for later use
      $currentIndentation = strspn($line, " ");
  
      #Go though list of unclosed tags and recursivley close all that need to be closed
      foreach ($unclosed_tags as $lineNum =&gt; $tag) {
          #Assign a variable to the tag indentation
          $tagIndentation = $tag[1];
  
          echo $tag[0] . "$tagIndentation:$currentIndentation=" . !($tagIndentation &lt; $currentIndentation) . "&lt;br&gt;";
  
          #Check if the current indentation is not smaller than the tag
          if (!($tagIndentation &lt; $currentIndentation)) {
              #Close the tag
              $output .= "&lt;/" . $tag[0] . "&gt;";
              #Remove the tag from unclosed list cince we just closed it
              $unclosed_tags = array_diff($unclosed_tags, array($unclosed_tags[$key]));
          }
      }
  
      #Get the current element (== the first string in a string)
      $currentElement = preg_replace('/(\s*)([^\s]*)(.*)/', '$2', $line);
  
      #Output as open tag
      $output .= "&lt;" . $currentElement . "&gt;" . trim(str_replace_first($currentElement, "", $line));
      #Add to list of unclosed tags
      $unclosed_tags[$lineNum] = array($currentElement, $currentIndentation);
  }
  </code></pre>
  
  <p>The sample .pug file I'm trying to parse looks like this:</p>
  
  <pre><code>html
    head
      title Test
    body
      h1 This page was defined using JADE (PUG)!
      h2 This a Header 2
      div
        p &lt;strong&gt;This text will appear strong&lt;/strong&gt;
        hr
        br
      ul
        li This
        li is a 
        li test
      ol
        li &lt;b&gt;Ordered Test&lt;/b&gt;
        li for the win
  </code></pre>
  
  <p>The exported html looks like this</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Test&lt;/title&gt;
  
      &lt;body&gt;
          &lt;h1&gt;This page was defined using JADE (PUG)!&lt;/h1&gt;
          &lt;h2&gt;This a Header 2&lt;/h2&gt;
          &lt;div&gt;
              &lt;p&gt;&lt;strong&gt;This text will appear strong&lt;/strong&gt;&lt;/p&gt;
              &lt;hr/&gt;
              &lt;/p&gt;
              &lt;br/&gt;
              &lt;/p&gt;
              &lt;ul&gt;
                  &lt;li&gt;This&lt;/li&gt;
                  &lt;li&gt;is a&lt;/li&gt;
                  &lt;li&gt;test&lt;/li&gt;
                  &lt;ol&gt;
                      &lt;li&gt;&lt;b&gt;Ordered Test&lt;/b&gt;&lt;/li&gt;
                      &lt;li&gt;for the win&lt;/li&gt;
  </code></pre>
  
  <p>Appart from closing the unclosed tags this is my complete algorithm, but it is not working since sometimes tags are not closed because the inner loop can't check if more tags need to be closed this cycle!</p>
  
  <p><strong>I am not looking for a existing library, but rather tips on why my code is not working and how to improve upon the code quality and execution time!</strong></p>