2
<?php

$mytext = <<<EOF
test {
    gl: 100;
    mkd: 0;
    sld: 0;
}
EOF;



echo $mytext;

?>

the output is :

test { aaa: black; bbb: yellow; ccc: red; }

and I want the output exactly how it's wrote inside the eof segments.

test {
    gl: 100;
    mkd: 0;
    sld: 0;
}

Any idea ?

  • 3
    Don't look in your browser: that will collapse _any_ sequential whitespace to 1 space. Look the _source_ of the page: is that the output you want? – Wrikken Feb 28 '14 at 13:56
  • Yes! On the source it looks fine and this is what I need BUT want to display the end results for the user :) I thought it would be extremely easy :D –  Feb 28 '14 at 14:02
  • If you're sending this output to the _browser_, it will ignore EOL, collapse whitespace, and glue together fragments. You will need to explicitly add `
    `'s or use `
    ` to force multiple lines.
    – Phil Perry Feb 28 '14 at 14:39

2 Answers2

2

Use <pre> to format the text like that:

<?php

$mytext = <<<EOF
<pre>
test {
    gl: 100;
    mkd: 0;
    sld: 0;
}</pre>
EOF;



echo $mytext;

?>
Shlomo
  • 3,880
  • 8
  • 50
  • 82
  • I need to keep the EXACT format since the input would be by the user. –  Feb 28 '14 at 13:57
  • Yes, you get the userinput in `$strInput` then do: `$strInput = 'test { gl: 100; mkd: 0; sld: 0; }'; $mytext = << $strInput EOF;` – Shlomo Feb 28 '14 at 14:00
1

Browser usually ignores control characters, like whitespaces in HTML.

Another solution would be using echo(nl2br($mytext)) instead. Which would convert all \n into <br />.

Converting manually might help You dealing with another kind of whitespaces. Something like this:

$replacee = array("\n", "\t");
$replacement = array("<br />", "    ");
$mytext = str_replace($replacee, $replacement, $mytext);

Really similar problem linked here and here (mostly client side solutions).

Community
  • 1
  • 1
Kamiccolo
  • 7,758
  • 3
  • 34
  • 47