Skip to content

Instantly share code, notes, and snippets.

@trajche
Last active November 5, 2019 19:56
Show Gist options
  • Save trajche/45425b0b845fee213377c5103a212ba2 to your computer and use it in GitHub Desktop.
Save trajche/45425b0b845fee213377c5103a212ba2 to your computer and use it in GitHub Desktop.
link rel parser for headers in shopify / github
function returnHeaderArray($linkHeader) {
$cleanArray = [];
if (strpos($linkHeader, ',') !== false) {
//Split into two or more elements by comma
$linkHeaderArr = explode(',', $linkHeader);
} else {
//Create array with one element
$linkHeaderArr[] = $linkHeader;
}
foreach ($linkHeaderArr as $linkHeader) {
$cleanArray += [
extractRel($linkHeader) => extractLink($linkHeader)
];
}
return $cleanArray;
}
function extractLink($element) {
if (preg_match('/<(.*?)>/', $element, $match) == 1) {
return $match[1];
}
}
function extractRel($element) {
if (preg_match('/rel="(.*?)"/', $element, $match) == 1) {
return $match[1];
}
}
//This was coded during websummit :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment