0

This should be easy, but I cannot get there.

I am writing a program in php for a friend to put his poetry online. The poems are stored in MySql. The problem is carriage returns. When I use phpMyAdmin the lines are formatted correctly, but when I use echo in php the whole poem comes out as a string of text.

I am uploading the poetry using

$poetry = htmlentities($_POST['Poetry']); 

The problem seems to be getting the data from sql and displaying it.

It would be helpful if I could see the exact characters (including ALL entities such as carriage returns) that php is supplying to the browser (Chrome).

Any ideas?

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • You can do Ctrl+U in Chrome, or select "View Source" from the menu. That would show you the raw output from your server. If you paste that into an editor such as Notepad++ and select View->Show Symbol->Show All Characters in that editor, it'll show you everything. – ADyson Nov 24 '21 at 15:12
  • But remember... in HTML markup, a line break is done by writing a `
    ` tag. HTML doesn't take any notice of the kinds of newlines used in text files, such as `\n`
    – ADyson Nov 24 '21 at 15:12
  • 1
    Are you looking for `nl2br()`? – Nico Haase Nov 24 '21 at 15:14
  • 1
    P.S. `$poetry = htmlentities($_POST['Poetry']);`...why are you doing that? You should never sanitise _input_ like that. Your input code cannot possibly know about all the places where the data could be used or displayed later. There are plenty of output environments where HTML tags would not be an issue, since they wouldn't be parsed. You should only sanitise data at the point of _output_, once you know what the output target is, and you can apply the correct kind of sanitisation. – ADyson Nov 24 '21 at 15:14
  • Bear in mind also of course that htmlentities will encode any HTML in the provided string, and prevent it from being parsed as HTML by the browser. That's good for preventing XSS, but not so good if your string includes HTML (such as "br" tags, for example!) which you were expecting to be treated as valid markup. – ADyson Nov 24 '21 at 15:18
  • Thank you everybody for replying. Perhaps I should make my question simpler. How do I allow someone to type four or more lines of poetry and then present those lines in a web page without them becoming a word salad? – Max Kite Nov 24 '21 at 20:16
  • Feel free to add all clarification to your question by editing it. Usually, printing database content does not lead to word salad – Nico Haase Nov 24 '21 at 20:56
  • 1
    It depends how you're entering it, what the content is, what precisely you do with it afterwards. Rather than asking something as broad as that with 20 possible answers, most of which might not suit your exact scenario, please show us the whole process of what _you're_ doing currently which is leading to this problem. Provide a [mre] of the code and raw data involved. Then we can give you precise help instead of generic guidance, speculation and guesswork. Thanks. – ADyson Nov 25 '21 at 06:57
  • Having said that, this will mainly hinge, to be honest, on whether the raw input is using newlines or br tags to denote a line break. – ADyson Nov 25 '21 at 07:04
  • Thank you A Dyson and everyone for your comments. The problem is so simple but I can't see a way out. There is a program for my friend to upload his poetry. At the end of each line, he will hit the return key on his keyboard. Although the CR is recognized in MySQL, it is not when the text is displayed in html. I need to convert the CR to
    but don't know how to.
    – Max Kite Nov 25 '21 at 10:53
  • Have you tried anything to convert the linefeeds? Anything not working with `nl2br()` which was built for exactly that problem? – Nico Haase Nov 25 '21 at 11:08
  • `I need to convert the CR to
    but don't know how to`...Nico Haase already suggested how to do that yesterday - did you follow up that comment by investigating the suggested function?
    – ADyson Nov 25 '21 at 11:14
  • 1
    `the CR is recognized in MySQL`...this doesn't make any sense. mySQL is a database engine, not a graphical display system. It doesn't interpret, format, or display text, it just stores it. Perhaps you're talking about how the phpMyAdmin client application interprets the data. Other mysql clients may or may not display the data in the same way. Please understand you ensure you understand the difference between backend storage and frontend display. – ADyson Nov 25 '21 at 11:16
  • No htmlentities

    this is line one This is line two this is line three this is line four >>>>>>This is what I see when I do what you suggest and copy/paste (into SciTE) which is correct. When I request 'see end of line' it puts CRLF at the end of each line in grey boxes. How do I convert CRLF to
    ?
    – Max Kite Nov 25 '21 at 11:19
  • `How do I convert CRLF to
    `... again, see https://stackoverflow.com/questions/70098443/how-can-i-see-html-control-entities-in-chrome?noredirect=1#comment123914721_70098443 and also https://stackoverflow.com/questions/70098443/how-can-i-see-html-control-entities-in-chrome?noredirect=1#comment123914721_70098443
    – ADyson Nov 25 '21 at 11:20
  • and also https://stackoverflow.com/questions/70098443/how-can-i-see-html-control-entities-in-chrome?noredirect=1#comment123914721_70098443 . And then consult the PHP manual for that function. Or alternatively type "php How do I convert CRLF to
    " [into google](https://www.google.com/search?q=php+How+do+I+convert+CRLF+to+%3Cbr+%2F%3E) and get the necessary information instantly, without needing to wait for people to spoon-feed you. :-)
    – ADyson Nov 25 '21 at 11:22
  • 1
    **Please add all clarification to your question by editing it**. Don't use the comment section for important stuff – Nico Haase Nov 25 '21 at 11:23
  • Please provide enough code so others can better understand or reproduce the problem. – Community Nov 28 '21 at 13:06

0 Answers0