You are here

function httprl_get_ranges in HTTP Parallel Request & Threading Library 7

Same name and namespace in other branches
  1. 6 httprl.module \httprl_get_ranges()

Parse a range header into start and end byte ranges.

Parameters

string $input: String in the form of bytes=0-1024 or bytes=0-1024,2048-4096

Return value

array Keyed arrays containing start and end values for the byte ranges. Empty array if the string can not be parsed.

1 call to httprl_get_ranges()
httprl_send_request in ./httprl.module
Perform many HTTP requests.

File

./httprl.module, line 2023
HTTP Parallel Request Library module.

Code

function httprl_get_ranges($input) {
  $ranges = array();

  // Make sure the input string matches the correct format.
  $string = preg_match('/^bytes=((\\d*-\\d*,? ?)+)$/', $input, $matches) ? $matches[1] : FALSE;
  if (!empty($string)) {

    // Handle multiple ranges.
    foreach (explode(',', $string) as $range) {

      // Get the start and end byte values for this range.
      $values = explode('-', $range);
      if (count($values) != 2) {
        return FALSE;
      }
      $ranges[] = array(
        'start' => $values[0],
        'end' => $values[1],
      );
    }
  }
  return $ranges;
}