Search code examples
phpdata-uri

How can I get some text from data uri and assign to an array?


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?


Solution

  • 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=');