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:
- Use the PHP function
nl2br
to convert all line breaks to HTML<br>
tags. - 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
Using Square Brackets in Hidden HTML Input Fields
How to Create Static Classes in PHP (Like in C#)
Artisan Migrate Could Not Find Driver
How to Get Previous Month and Year Relative to Today, Using Strtotime and Date
Failed to Open Stream: Http Wrapper Does Not Support Writeable Connections
Ajax Post Request in Laravel 5 Return Error 500 (Internal Server Error)
Get the Keys for Duplicate Values in an Array
Skip First Line of Fgetcsv Method
Warning: MySQLi_Query(): Couldn't Fetch MySQLi
How to Return Back Twice in Laravel
Parametrized Pdo Query and 'Limit' Clause - Not Working
Why Is Textarea Filled with Mysterious White Spaces
Laravel Eloquent: How to Order Results of Related Models
Is Is Possible to Set a Default Pdo Fetch Mode