Search code examples
phphtmlnewlinetextinput

Getting CRLF from text input in php


I am relatively new to web development. I have a simple text input and submit button in html.

<div>
<input type="text" name="sender" id="sender" class="form-control" placeholder="" />
<input type="submit" class="btn-u btn-u-small" name="send" value="Send email" />
</div>

I get the value of the text input in php and echo it.

<?php
if(isset($_POST["send"]))
{
    echo $_POST["sender"] . " \r\nThis was a test";
}
?>

Now I type \r\nUser input in text input. I intend the echo command to look like:

echo "\r\rUser input." . " \r\nThis was a test."

And when displayed, should look like:

User input. This was a test.

Instead the first \r\n prints as it is and only the second \r\n is interpreted as CRLF.

\r\nUser input. This was a test.

I've tried '\r\n' "\r\n" \\r\\n but non seem to work.

Note: I don't want actual line break in html, I know that in html line breaks are <br />. I want a line break in php string.


Solution

  • The browser is going to filter/encode/sanitize everything you enter in the text field, so you will have to find a way to make the browser generate CRLF for you.

    One way would be to:

    • use Firefox
    • install extension TamperData
    • use it to modify the text you send, e.g. inserting %0D%0A at the beginning; which is the URL-encoded version of \r\n