views:

60

answers:

5

Hi

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)?

+3  A: 

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.

Daniel Vandersluis
thanks, I ended up using chunk_split :D
Alex
+2  A: 

Certainly. White space is ignored:

<?php

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

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

?>
Álvaro G. Vicario
+2  A: 
base64_decode(wordwrap(base64_encode('very ... long'), 80, "\n", true)) == 'very ... long'
maat
+1  A: 

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>
ircmaxell
but it doesnt work in Opera...
Alex