-2

I have a text where I want to bypass using the css property white-space:

white-space: pre-line

How can I replace white-spaces in a text with a break (<br>) in PHP so that I have the same output like using white-space: pre-line?

Example:

GET / HTTP/1.1 Referer: http://192.168.2.154/ Cookie: PHPSESSID=s0p0vc93nf630ornkvdprgc5ko;security=impossible Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding: gzip,deflate Host: 192.168.2.154 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36 Connection: Keep-alive

How can I format this code without css (white-space) that I have it like this:

GET / HTTP/1.1
Referer: http://192.168.2.154/
Cookie: PHPSESSID=s0p0vc93nf630ornkvdprgc5ko;security=impossible
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate
Host: 192.168.2.154
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
Connection: Keep-alive
itranger
  • 175
  • 2
  • 13
  • 1
    Where is your php attempt that is not working? – B001ᛦ Nov 04 '20 at 17:14
  • @B001ᛦ Sorry... What I've tried: str_replace(' ', '
    ', $text);
    – itranger Nov 04 '20 at 17:16
  • CSS's `white-space` attribute is telling the browser how the content should be rendered in the browser. PHP doesn't render anything since it's server side, so PHP can't replace CSS attributes like this. – M. Eriksson Nov 04 '20 at 17:16
  • Is there any possibility to render it first and then to replace the white-spaces and get the new code with the replaced white-spaces? – itranger Nov 04 '20 at 17:18
  • 1
    You really need to give us a proper example if you want us to be able to give you proper and relevant suggestions. Your question is a too unclear atm. Please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) and edit your question accordingly. – M. Eriksson Nov 04 '20 at 17:18
  • @MagnusEriksson I have add an example, I hope it's now better to understand – itranger Nov 04 '20 at 17:29
  • The example (all on one line), is that what it looks like in the browser? Does it look correct if you check the source (right click on the page and click on "view source"?) If it's just in the browser it looks like one line, it could be that it has line breaks (which browsers don't render). In that case, try: `$text = nl2br($text);`. If you really has it as only a one-liner, then it's a bit trickier. – M. Eriksson Nov 04 '20 at 17:35
  • So far I see a sample bit of code that is CSS, and an HTTP request and response. Where is the PHP? Why doesn't your `str_replace` work. Have you tried [`preg_replace`](https://stackoverflow.com/a/2109339/231316)? – Chris Haas Nov 04 '20 at 17:36

1 Answers1

1

My strong guess is that the PHP generated response is already on multiple lines, but the browser is rendering it in "HTML mode" (because of the default text/html content type), thus line-breaks are transformed into single spaces.

Depending on your use-case, you can either use function nl2br() or set the text/plain content type for the response:

header('Content-type: text/plain');
Zoli Szabó
  • 4,366
  • 1
  • 13
  • 19