Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - add/remove carriage returns to a base 64 encoded string

I have a verrrrrrrry long base64 encoded string which is displayed in a textarea. The problem is that this string doesn't have any spaces or carriage returns so it gets displayed on one line with a ugly horizontal scroll bar.

Can I add somehow carriage returns manually after base64_encode() and before output to the textarea, then remove these CR's after retrieving this string from the textarea (before decoding)?

like image 654
Alex Avatar asked Dec 16 '22 21:12

Alex


2 Answers

You could use chunk_split to split your string into chunks of a specific length, and then rejoin those chunks with the character or string you want:

$str = base64_encode($whatever);
echo '<textarea name="textarea">'
     . chunk_split($str, 30, "\n") // put a newline every 30 characters
     . '</textarea>';

As base64_decode ignores whitespace, you don't need to do anything special once you retrieve the textarea value.

like image 68
Daniel Vandersluis Avatar answered Jan 31 '23 08:01

Daniel Vandersluis


Certainly. White space is ignored:

<?php

echo base64_encode('Lorem Ipsump'); // TG9yZW0gSXBzdW1w

echo base64_decode('TG9 yZW0gS      XBz dW1w'); // Lorem Ipsump

?>
like image 28
Álvaro González Avatar answered Jan 31 '23 07:01

Álvaro González