[^,.]*),(?.*)$#'; /** * @throws InvalidDataUriException */ public function parse(string $dataUri): DataUri { $matches = []; if (preg_match(self::PATTERN, $dataUri, $matches) !== 1) { throw new InvalidDataUriException(); } if ($matches['media_type'] === '') { $items = []; } else { $items = explode(';', $matches['media_type']); } $mediaType = 'text/plain'; $parameters = ['charset' => 'US-ASCII']; $base64 = false; if (count($items) > 0) { $mediaType = array_shift($items); foreach ($items as $item) { if ($item === 'base64') { $base64 = true; continue; } if (str_contains($item, '=')) { [$key, $value] = explode('=', $item); $parameters[$key] = $value; } } } return new DataUri( $mediaType, $parameters, $base64, $matches['data'] ); } }