- http://tools.ietf.org/html/rfc5988
- https://developer.github.com/v3/#pagination
- https://gist.github.com/niallo/3109252
function parse_link_header(header) {
if (header.length === 0) {
throw new Error("input must not be of zero length");
}
// Split parts by comma
var parts = header.split(',');
var links = {};
// Parse each part into a named link
for(var i=0; i<parts.length; i++) {
var section = parts[i].split(';');
if (section.length !== 2) {
throw new Error("section could not be split on ';'");
}
var url = section[0].replace(/<(.*)>/, '$1').trim();
var name = section[1].replace(/rel="(.*)"/, '$1').trim();
links[name] = url;
}
return links;
}
function parseLinkHeader($header) {
if (strlen($header) == 0) {
throw new \Exception("input must not be of zero length");
}
$parts = explode(',', $header);
$links = [];
foreach($parts as $p) {
$section = explode(';', $p);
if (count($section) != 2) {
throw new \Exception("section could not be split on ';'");
}
$url = trim(preg_replace("/<(.*)>/", '$1', $section[0]));
$name = trim(preg_replace("/rel=\"(.*)\"/", '$1', $section[1]));
$links[$name] = $url;
}
return $links;
}