1

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.

code-8
  • 54,650
  • 106
  • 352
  • 604
  • `'\r\n'` is not the same as `"\r\n"`. `nl2br()` does not replace anything, it _adds_ something. https://stackoverflow.com/a/63892752/2943403 – mickmackusa Feb 10 '22 at 15:28

2 Answers2

1

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();
    } 
}
dbiagi
  • 85
  • 9
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(' ', '&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>';
Mátyás Grőger
  • 1,207
  • 2
  • 13
  • 24
  • let me try now; – code-8 Feb 10 '22 at 14:41
  • It gave the same look, no line breaks... – code-8 Feb 10 '22 at 14:43
  • I udpated the answer, first I wrote it wrongly, first parameter should be the PHP EOL second one the br – Mátyás Grőger Feb 10 '22 at 14:43
  • Ok, trying, again. – code-8 Feb 10 '22 at 14:43
  • Perfect works now. I update my site, and verified also works : https://www.bunlongheng.com/raw/N2U5NWYzNGMtOGI5Yy00MjFhLTkzNzktOGI1ZWExZmE1ZGNm – code-8 Feb 10 '22 at 14:45
  • 1
    One thing to note, it only works for new lines. Is there a way to maintain the actual format like Tabs of the original text file? My goal is to display exactly what I uploaded. Think about it If I copied the file and uploaded it as-is, I want to display it as-is. Right now, it is not : See it here : https://www.bunlongheng.com/raw/YTkxYTUzMzItODFkMy00MjA5LTgwYTUtMDNiMTA4ZTBjZmRi – code-8 Feb 10 '22 at 14:48
  • I updated my answer – Mátyás Grőger Feb 10 '22 at 15:00
  • I tried the pre one and uploaded my entire file. please see the diff with the upload one was the raw one. See this https://www.bunlongheng.com/paste/NWJiYzc4YzUtMzMxZi00Njg4LTk4YTMtOTQ3YjdhMTVkYzZk and raw one https://www.bunlongheng.com/raw/NWJiYzc4YzUtMzMxZi00Njg4LTk4YTMtOTQ3YjdhMTVkYzZk – code-8 Feb 10 '22 at 15:21
  • I updated my site to test your codes. – code-8 Feb 10 '22 at 15:22
  • Look at pastebin rendered perfectly as I uploaded : https://pastebin.com/raw/zSi6itA2 – code-8 Feb 10 '22 at 15:24
  • `return str_replace('\t', '&nbsp&nbsp&nbsp&nbsp', str_replace(' ', '&nbsp', str_replace(PHP_EOL, '
    ', $paste->raw)));` this doesn't render well at al.. Just my feedback to you.
    – code-8 Feb 10 '22 at 15:28
  • I will try your latest answer now `return '
    '.$paste->raw.'
    ';`
    – code-8 Feb 10 '22 at 15:28
  • 1
    dont try it as it will not be good, you need to change < and > chars to < and > in the string to achieve the desired result – Mátyás Grőger Feb 10 '22 at 15:31
  • I uploaded my entire controller to test, and lines are missing : https://i.imgur.com/nTDQ4W7.png – code-8 Feb 10 '22 at 15:31
  • That's a lot of work.. to just reurn a plain text what on what we query. – code-8 Feb 10 '22 at 15:31