title: Why output control doesn't catch sql?
body: <p>I wrote a code which is changing headings h1 to h2 if it's not first heading of that type. It works, but only when I'm using just PHP. If I want to use it on website, which one is using database to output content, that code doesn't work.</p>
  <p>I'm using ob_start() before <code>&lt;html&gt;</code> and ob_get_contents() + ob_end_clean() + rest of my code after <code>&lt;/html&gt;</code>, so I think that has to be something wrong &quot;catching&quot; content from database, but I'm not sure. I tried to use that code on a website based on WordPress.</p>
  <p>My code (I know it isn't probably the best solution, but it works when I'm using it on any website without CMS):</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;?php
      ob_start();
  ?&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;random page&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
        &lt;div class=&quot;container&quot;&gt;
          &lt;div class=&quot;row&quot;&gt;
            &lt;div class=&quot;col-md-5&quot;&gt;
              &lt;h1&gt;afdsfdassfdfdsa&lt;/h1&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;h1&gt;sadffsadf afdsfdsa afsdfs?&lt;/h1&gt;
        &lt;h1 class=&quot;superclass&quot;&gt;random text?&lt;/h1&gt;
        &lt;div&gt;
            &lt;p&gt;some ending content&lt;/p&gt;
        &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;?php
      $bufferContent = ob_get_contents();
      ob_end_clean();
  
      $matchedContent = '';
      $endContent = '';
      $modifiedContent = '';
      $firstHeadingBoolean = true;
      $h1Pattern = array();
      $h1Pattern[0] = '%&lt;h1(.*?)&gt;%';
      $h1Pattern[1] = '%&lt;/h1&gt;%';
      $h1Replacement = array();
      $h1Replacement[0] = '&lt;h2$1&gt;';
      $h1Replacement[1] = '&lt;/h2&gt;';
      if(preg_match_all('%((.|\n)*?)(&lt;h1.*?&gt;.*?&lt;/h1&gt;)%', $bufferContent, $contentMatches)){
          foreach($contentMatches[0] as $matches) {
              $matchedContent .= $matches;
          }
          $endContent = str_replace($matchedContent, '', $bufferContent);
  
          foreach ($contentMatches[0] as $matches) {
              if(!$firstHeadingBoolean){
                  $firstHeadingBoolean = false;
              } else {
                  $matches = preg_replace($h1Pattern, $h1Replacement, $matches);
              }
              $modifiedContent .= $matches;
          }
          echo $modifiedContent;
          echo $endContent;
      } else {
          echo $bufferContent;
      }
  ?&gt;
  </code></pre>
  <p>EDIT: I tried to use solutions from there but nothing has changed: <a href="https://stackoverflow.com/questions/772510/wordpress-filter-to-modify-final-html-output">WordPress filter to modify final html output</a>
  Now, after some testing, I can see it's not working because preg_match_all doesn't work correctly. Anyone has an idea what is wrong with that preg_match_all? I tested that regex pattern inside that in regex101 and on my localhost and everything worked fine. I don't understand, why isn't it working here?</p>