<?php
function dataFromUrl($url) {
$url = trim($url);
if (!preg_match('@^data:[^,;]*+(?>\s*+;\s*+[^=,;]++\s*+=\s*+[^,;]*+\s*+)*+\s*+(?<type>;\s*+base64\s*+,\s*+|,)(?<data>.*)$@ADis', $url, $m)) {
throw new \Exception('Bar data URL.');
}
$data = rawurldecode($m['data']);
if (($m['type'] ?? '') !== ',') $data = base64_decode($data);
return $data;
}
var_dump(dataFromUrl('data:text/plain;charset=utf-8;base64,SGVsbG8gd29ybGQh')) . PHP_EOL;
var_dump(dataFromUrl('data:text/plain ; charset = utf-8 ; base64, SGVsbG8gd29ybGQh')) . PHP_EOL;
var_dump(dataFromUrl('data:; charset = utf-8 ; base64, SGVsbG8gd29ybGQh')) . PHP_EOL;
var_dump(dataFromUrl('data:;base64 , SGVsbG8gd29ybGQh')) . PHP_EOL;
var_dump(dataFromUrl('data:;base64 , SGVsbG8gd29ybGQh')) . PHP_EOL;
var_dump(dataFromUrl('data:text/plain;charset=utf-8,Hello%20world%21')) . PHP_EOL;
preferences:
164.16 ms | 409 KiB | 5 Q