'Rendering plain text via PHP after query from database [duplicate]
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.
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.
Solution 1:[1]
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(' ', ' ', $string);
str_replace('\t', '    ', $string);
So all together:
return str_replace('\t', '    ', str_replace(' ', ' ', 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('>', '>', str_replace('<', '<', $paste->raw)).'</pre>';
Solution 2:[2]
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();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |

