From 1ec230746126e02a92b1d835acfdf21fdd33ec1e Mon Sep 17 00:00:00 2001 From: Mardix Date: Sat, 1 Mar 2014 13:02:54 -0500 Subject: [PATCH] Added method parseTagAttributes to easily read key value attributes --- src/Handlebars/Helpers.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Handlebars/Helpers.php b/src/Handlebars/Helpers.php index 369b0a4..78ad530 100755 --- a/src/Handlebars/Helpers.php +++ b/src/Handlebars/Helpers.php @@ -703,4 +703,33 @@ private function extractSlice($string) } return [$m[1], $slice_start, $slice_end]; } + + /** + * Break an argument string of HTML attribute/values into an array of key value pairs + * + * @param string $string Argument String as passed to a helper + * @return array the argument list as an array + * @credit: https://github.com/Opine-Org/handlebars.php/commit/c749883e36ea6807824d7a2829cdcc02b11d49ad + */ + public function parseTagAttributes ($string) + { + $attributes = []; + $pattern = '#(?(DEFINE) + (?[a-zA-Z][a-zA-Z0-9-:]*) + (?"[^"]+") + (?\'[^\']+\') + (?[^\s>]+) + (?((?&value_double)|(?&value_single)|(?&value_none))) + ) + (?(?&name))(=(?(?&value)))?#xs'; + + if (preg_match_all($pattern, $string, $matches, PREG_SET_ORDER)) { + foreach ($matches as $match) { + $attributes[$match['n']] = isset($match['v']) + ? trim($match['v'], '\'"') + : null; + } + } + return $attributes; + } }