Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I find it odd to have huge HTML strings, then using some string search and replace hack to format that afterwards...</p> <p>When constructing HTML with PHP, I like using arrays :</p> <pre><code>$htmlArr = array(); foreach ($dataSet as $index =&gt; $data) { $htmlArr[] = '&lt;p&gt;Line#'.$index.' : &lt;span&gt;' . $data . '&lt;/span&gt;&lt;/p&gt;'; } $html = implode("\n", $htmlArr); </code></pre> <p>This way, every HTML line has it's separate $htmlArr[] value. Moreover, if you need your HTML to be "pretty print", you can simply have some sort of method that will indent your HTML by prepending whitespaces at the beginning of every array elements depending on somme rule set. For example, if we have :</p> <pre><code>$htmlArr = array( '&lt;ol&gt;', '&lt;li&gt;Item 1&lt;/li&gt;', '&lt;li&gt;&lt;a href="#"&gt;Item 2&lt;/a&gt;&lt;/li&gt;', '&lt;li&gt;Item 3&lt;/li&gt;', '&lt;/ol&gt;' ); </code></pre> <p>Then the formatting function algorithm would be (very simple one, considering that the HTML is well constructed) :</p> <pre><code>$indent = 0; // initial indent foreach &amp; $value in $array $open = count how many opened elements $closed = count how many closed elements $value = str_repeat(' ', $indent * TAB_SPACE) . $value; $indent += $open - $closed; // next line's indent end foreach return $array </code></pre> <p>Then <code>implode("\n", $array)</code> for the prettyfied HTML</p> <p>** <strong>UPDATE</strong> **</p> <p>After the question edit by Felix Kling, I realize that this has nothing to do with the question. Sorry about that :) Thanks though for the clarification.</p>
 

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