arrow
arrow

Reputation: 174

Tabulation for multiple line string PHP

Let's say you have a php string that has multiple html lines, for example:

$string = "<p>
   Vestibulum ac diam sit amet
</p>
<p>
   Vestibulum ac diam sit amet
</p>
<p>
   Vestibulum ac diam sit amet
</p>";

And I'm displaying it this way:

        <div>
            <div>
                <?php echo $string . PHP_EOL; ?>
            </div>
        </div>

How can I get the paragraphs to have the same tabulation as the opening php tag? I know I can add tabs in the $string, but if I'm using this string in multiple places, I may not want the same tabulation for each place.

What I get is this:

            <div>
                <div>
                    <p>
    Vestibulum ac diam sit amet
</p>
<p>
    Vestibulum ac diam sit amet
</p>
<p>
    Vestibulum ac diam sit amet
</p>
                </div>
            </div>

What I want is this:

            <div>
                <div>
                    <p>
                        Vestibulum ac diam sit amet
                    </p>
                    <p>
                        Vestibulum ac diam sit amet
                    </p>
                    <p>
                        Vestibulum ac diam sit amet
                    </p>
                </div>
            </div>

Upvotes: 2

Views: 599

Answers (2)

Mikołaj Rozwadowski
Mikołaj Rozwadowski

Reputation: 1037

HTML indentation doesn't really matter and, as people said in the comments, makes the code unnecessarily bigger. However, if you really want to indent it, you can use this function:

function indent_html($code, $num)
{
    $tabs = str_repeat("\t", $num); // or spaces if you want
    return $tabs.str_replace("\n", "\n$tabs", $code);
}

Upvotes: 2

Jean-philippe Emond
Jean-philippe Emond

Reputation: 1614

you may try something like:

tidy_get_output();

reference

Upvotes: 0

Related Questions