Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
Rendering plain text via PHP after query from database

I have this dump raw plain text. I uploaded it into my site as is

➜  Desktop ping 198.54.120.122                                          
PING 198.54.120.122 (198.54.120.122): 56 data bytes
64 bytes from 198.54.120.122: icmp_seq=0 ttl=35 time=85.655 ms
64 bytes from 198.54.120.122: icmp_seq=1 ttl=35 time=84.976 ms
64 bytes from 198.54.120.122: icmp_seq=2 ttl=35 time=85.856 ms
64 bytes from 198.54.120.122: icmp_seq=3 ttl=35 time=85.103 ms
^C
--- 198.54.120.122 ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 84.976/85.397/85.856/0.368 ms

I query it back I got this

I dd() it out to test, meaning at this point, I didn't lost my new lines yet.

enter image description here

I've tried

str_replace('\r\n','<br>',$paste->raw); and hoping to see rendering better

public function raw($uuid)
{
    $paste  = Paste::where('uuid', base64_decode($uuid))->first();
    return str_replace('\r\n','<br>',$paste->raw);
}

Result

It rendering like this without newlines

PING 198.54.120.235 (198.54.120.235): 56 data bytes 64 bytes from 198.54.120.235: icmp_seq=0 ttl=35 time=85.655 ms 64 bytes from 198.54.120.235: icmp_seq=1 ttl=35 time=84.976 ms 64 bytes from 198.54.120.235: icmp_seq=2 ttl=35 time=85.856 ms 64 bytes from 198.54.120.235: icmp_seq=3 ttl=35 time=85.103 ms ^C --- 198.54.120.235 ping statistics --- 4 packets transmitted, 4 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 84.976/85.397/85.856/0.368 ms

You guys can even see it live : here

How would one achieve that? Should I use the front-end to style this?

Imagine, you view a raw file in GitHub or gist , I'm trying to build a mini version of that, an ability to display what I uploaded, and quickly share with others via a link.

The link suggested above, doesn't answer what I am looking for.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

To keep the line breaks (\n, \r\n) in html you could use the nl2br php function.

Your raw function would be something like this

public function raw($uuid)
{
    $paste  = Paste::where('uuid', base64_decode($uuid))->first();
    return nl2br($paste->raw);
}

Or you could use the <pre> tag to keep the original format.

Or you could serve the file as plain text using the header function.

Add this to your code before any output, in a controller for example:

header('Content-Type: text/plain');

One thing to note is that if you're using some framework it wil probably have some helper service to add headers. But adding this code above should work either way. And since you are serving plain text files, no html tag will work.

Adapting to your code:

class OutputController {
    public function output() {
        header('Content-Type: text/plain');

        echo Paste::where('uuid', base64_decode($uuid))->first();
    } 
}
about 4 years ago · Juan Pablo Isaza Report

0

Try to use the predefined constant PHP_EOL instead of \r\n(https://www.php.net/manual/en/reserved.constants.php#constant.php-eol)

return str_replace(PHP_EOL, '<br>', $paste->raw);

If you want to do the same for whitespaces, you can use the built in html codes for whitespaces, for example:

str_replace(' ', '&nbsp', $string); 
str_replace('\t', '&nbsp&nbsp&nbsp&nbsp', $string); 

So all together:

return str_replace('\t', '&nbsp&nbsp&nbsp&nbsp', str_replace(' ', '&nbsp', str_replace(PHP_EOL, '<br>', $paste->raw)));

Or there is another way, using the html <pre> and <code> tag. This tag makes is possible that the text will be displayed exactly as written in the HTML source code.

return '<pre>'.str_replace('>', '&gt;', str_replace('<', '&lt;', $paste->raw)).'</pre>';
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!