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开发者_StackOverflow 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)?
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.
Certainly. White space is ignored:
<?php
echo base64_encode('Lorem Ipsump'); // TG9yZW0gSXBzdW1w
echo base64_decode('TG9 yZW0gS XBz dW1w'); // Lorem Ipsump
?>
base64_decode(wordwrap(base64_encode('very ... long'), 80, "\n", true)) == 'very ... long'
Or you could simply use the HTML attribute wrap
to do it in pure HTML and bypass any PHP implementation...
<textarea name="foo" wrap="soft"><?php echo $encodedString; ?></textarea>
This can be done with the PHP function wordwrap.
精彩评论