Skip to content

Instantly share code, notes, and snippets.

@trajche
Last active Nov 5, 2019
Embed
What would you like to do?
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