Last active
November 5, 2019 19:56
-
-
Save trajche/45425b0b845fee213377c5103a212ba2 to your computer and use it in GitHub Desktop.
link rel parser for headers in shopify / github
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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