Why Does PHP Echo'D Text Lose Its Formatting

PHP File Contents as String - Keep Format

PHP isn't removing the spaces, the browser is doing it because you're echoing into an HTML web page. You need to tell the browser not to reformat it.

$contents = nl2br($contents);
echo "<pre>$contents</pre>";

You should also use the built-in nl2br() function to add <br> tags, rather than str_replace(). Although this isn't really needed inside <pre> tags.

PHP New Line Not Working

You are presumably echoing this onto a web page.

Browsers do not (or at least, should not) respect literal new lines, you have to use the HTML <br> tag instead.

Try this:

echo str_replace(array("\r\n","\r","\n"),'<br>',$message);
// or
echo nl2br($message);

PHP_EOL doesn't work on XAMPP

If you are viewing this in your browser, it's because browsers interpret newlines in HTML as regular space characters.

In HTML you need to use <br> for forcing a line break.

How do I keep whitespace formatting using PHP/HTML?

use the <pre> tag (pre formatted), that will use a mono spaced font (for your art) and keep all the white space

<pre>
text goes here and here
and here and here Some out here
▄ ▄█▄ █▄ ▄
▄█▀█▓ ▄▓▀▀█▀ ▀▀▀█▓▀▀ ▀▀ ▄█▀█▓▀▀▀▀▀▓▄▀██▀▀
██ ██ ▀██▄▄ ▄█ ▀ ░▒ ░▒ ██ ██ ▄█▄ █▀ ██
█▓▄▀██ ▄ ▀█▌▓█ ▒▓ ▒▓ █▓▄▀██ ▓█ ▀▄ █▓
█▒ █▓ ██▄▓▀ ▀█▄▄█▄▓█ ▓█ █▒ █▓ ▒█ ▓█▄ ▒
▀▒ ▀ ▀ █▀ ▀▒ ▀ █▀ ░

</pre>

You might have to convert any <'s to < 's

Format before mailing contents of .txt file using php

Does your text file actually contain HTML in it? If not, change $mail->isHTML(true) to $mail->isHTML(false) and then the email will be sent in plain text, which will make the line breaks in the text file stay in the email.

If the text file actually does contain HTML, you have two options:

  1. Use the PHP function nl2br to convert all line breaks to HTML <br> tags.
  2. Put <br> or <p>...</p> tags in the text file in the appropriate places.

How to replace \r\n with a line break

You don't use htmlentities on the way in, you use it on the output. You should be escaping to input into your database and then to display newlines on the output consider:

nl2br(htmlentities($comment));

Hide/Show toggle echo results in php

The single quotes in onclick='toggle_visibility('tog')' are conflicting with the outer single quotes. So either escape them or use double quotes in either the outer pair or the inner pair.

Then in PHP, remove the <?php echo. Just put the HTML in PHP. The echo only complicates things. If you need dynamic data in your HTML, just inject it at that place with <?php ... ?>. But so far I haven't seen any of that in your HTML: it is static.

Here is a working snippet, after having made that change in two places:

//turn entire div into togglefunction toggle_visibility(id) {
var e = document.getElementById(id); if (e.style.display == 'block' || e.style.display == '') e.style.display = 'none'; else e.style.display = 'block';}
<div id='first_product'>  <button onclick="toggle_visibility('tog')">Toggle</button>  <div id='tog'>    <div id='red_head'>      <p id='menu_title' class ='hidden' onclick="toggle_visibility('tog')">  Add your first menu item</p>    </div>    <h3 id='menu'>Menu Section</h3>    <form name='first_prod' id='first_prod' enctype='multipart/form-data' action='testing.php' method='POST' accept-charset='utf-8' >                     <label id='cat_label' name='cat_label'>Name</label>      <input type='text' id='cat_name' name='cat_name' value=''>      <label id='desc_label' name='desc_label'>Description</label>      <input type='text' id='cat_desc' name='cat_desc' value=''>    </form>  </div></div>


Related Topics



Leave a reply



Submit