Skip to content

Instantly share code, notes, and snippets.

@Caffe1neAdd1ct
Last active August 29, 2018 07:43
Show Gist options
  • Save Caffe1neAdd1ct/34636c6521f2efe3f537cf5d92448acf to your computer and use it in GitHub Desktop.
Save Caffe1neAdd1ct/34636c6521f2efe3f537cf5d92448acf to your computer and use it in GitHub Desktop.
Splitting get parameters by two delimiters
<?php
function getParamSplitBy($param, $delimiter='~', $subDelimiter=':')
{
$paramChunks = array_chunk(preg_split('/('.$delimiter.'|'.$subDelimiter.')/', urldecode($this->getParam($param))), 2);
$keys = array_column($paramChunks, 0);
$unpaddedValues = array_column($paramChunks, 1);
$keyCount = count($keys);
$unpaddedCount = count($unpaddedValues);
$values = ($keyCount > count($unpaddedCount)) ?
array_pad($unpaddedValues, $keyCount, '') :
$unpaddedValues;
$paramPairs = array_combine($keys, $values);
return $paramPairs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment