Skip to content

Commit

Permalink
Merge pull request #1732 from bolt/Add-url_decode-Twig-Filter
Browse files Browse the repository at this point in the history
Add `url_decode` Twig Filter
  • Loading branch information
I-Valchev authored Aug 19, 2020
2 parents 65cde78 + 29b5527 commit b2dc7d4
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Twig/TextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function getFilters(): array
new TwigFilter('ucwords', [$this, 'ucwords']),
new TwigFilter('preg_replace', [$this, 'pregReplace']),
new TwigFilter('format_bytes', [$this, 'formatBytes']),
new TwigFilter('url_decode', [$this, 'urlDecode']),
];
}

Expand Down Expand Up @@ -84,4 +85,23 @@ public function formatBytes(int $bytes, int $precision = 2): string

return round($bytes, $precision) . ' ' . $units[$pow];
}

public function urlDecode(string $string)
{
if (! mb_strpos($string, '=')) {
return urldecode($string);
}

$params = [];

foreach (explode('&', $string) as $chunk) {
$param = explode('=', $chunk);

if ($param) {
$params[urldecode($param[0])] = urldecode($param[1]);
}
}

return $params;
}
}

0 comments on commit b2dc7d4

Please sign in to comment.