You are here

function cf_http_split_response in Common Functionality 7.2

Same name and namespace in other branches
  1. 7 modules/cf_http/cf_http.module \cf_http_split_response()

Breaks apart an html formatted document string.

The string is broken into an array containing two parts: 'head' and 'body'. All other elements before, between, or after the html <head> and <body> tags are lost/ignored.

@see: cf_http_get_webpage()

Parameters

string $document: An http response string.

Return value

array An array containing the connection status and return http response. The array keys:

  • split: A boolean with TRUE representing that the document string was

successfully split, FALSE otherwise.

  • head: The HTML head code (if cf_dom is enabled, then this is empty).
  • body: The HTML body code (if cf_dom is enabled, then this is empty).
  • dom: If cf_dom is enabled, then this is the cf_dom object.

Related topics

File

modules/cf_http/cf_http.module, line 325
Common Functionality - HTTP module.

Code

function cf_http_split_response($document) {
  $results = array(
    'split' => FALSE,
    'head' => '',
    'body' => '',
    'dom' => NULL,
  );
  if (cf_is_empty_or_non_string('document', $document, WATCHDOG_ERROR)) {
    return $results;
  }

  // cf_dom is a new class that uses PHP DOM to more efficiently process markup.
  if (class_exists('cf_dom')) {
    $dom = new cf_dom(NULL, TRUE, TRUE, $document);
    if (!$dom
      ->is_loaded()) {
      return $results;
    }
    $dom
      ->set_doctype(variable_get('cf_dom_doctype', cf_dom::DOCTYPE));
    $results['dom'] = $dom;

    // @todo: optionally populate head and body tags for compatibility.
    $results['head'] = $dom
      ->get_markup(TRUE, FALSE);
    $results['body'] = $dom
      ->get_markup(TRUE);
  }
  else {
    $matches = array();
    $result = preg_match('/<head[^>]*>(.*)<\\/head>/sim', $document, $matches);
    if ($result > 0 && isset($matches[1])) {
      $results['head'] = $matches[1];
    }
    else {
      return $results;
    }
    $matches = array();
    $result = preg_match('/<body[^>]*>(.*)<\\/body>/sim', $document, $matches);
    if ($result > 0 && isset($matches[1])) {
      $results['body'] = $matches[1];
    }
    else {
      return $results;
    }
  }
  $results['split'] = TRUE;
  return $results;
}