0

my data

this1

this2

this3

this4

what I want

$keepit[0]='this1';
$keepit[1]='this2';
$keepit[2]='this3';
$keepit[3]='this4';

data uri

data:text/plain;charset=utf-8;base64,dGhpczENCnRoaXMyDQp0aGlzMw0KdGhpczQ=

Is it possible to do this?

Community
  • 1
  • 1
Ds Klur
  • 103
  • 1
  • 8

2 Answers2

1

You can do this with normal file methods using the data:// protocol:

$data = file('data://text/plain;charset=utf-8;base64,dGhpczENCnRoaXMyDQp0aGlzMw0KdGhpczQ=');

Alternatively though you can pull out the base64 encoded string and decode it yourself:

$plaintext = base64_decode('dGhpczENCnRoaXMyDQp0aGlzMw0KdGhpczQ=');
Steve H
  • 946
  • 9
  • 22
  • 'file_get_contents','file','base64_decode' ... what is the best solution ? – Ds Klur Apr 09 '14 at 09:12
  • The best option depends on your use case. If you are accepting data encoded files that may vary in encoding then the `data://` protocol is probably safest. If you're always just accepting plaintext base64 then `base64_decode` will likely be fastest, you will then need to explode the string it returns in to an array. – Steve H Apr 09 '14 at 09:28
0

If the encoded string can be decoded this will work:

$file_path = ''; // change this.
$fp = fopen($file_path, 'rb');
$contents = fread($handle, filesize($file_path));
fclose($fp);
$data_uri = preg_split('/,/', $contents);
$encoded = $data_uri[1];
$decoded = base64_decode($encoded);
var_dump($decoded);
Ryan
  • 14,392
  • 8
  • 62
  • 102