You are here

function views_break_phrase_string in Views (for Drupal 7) 7.3

Same name and namespace in other branches
  1. 6.3 includes/handlers.inc \views_break_phrase_string()

Break x,y,z and x+y+z into an array. Works for strings.

Parameters

string $str: The string to parse.

object $object: The object to use as a base. If not specified one will be created.

Return value

object An object containing

  • operator: Either 'and' or 'or'
  • value: An array of numeric values.
3 calls to views_break_phrase_string()
ViewsHandlersTest::test_views_break_phrase_string in tests/views_handlers.test
Tests views_break_phrase_string function.
views_handler_argument_string::query in handlers/views_handler_argument_string.inc
Build the query based upon the formula
views_handler_argument_string::title in handlers/views_handler_argument_string.inc
Get the title this argument will assign the view, given the argument.

File

includes/handlers.inc, line 1142
Defines the various handler objects to help build and display views.

Code

function views_break_phrase_string($str, &$handler = NULL) {
  if (!$handler) {
    $handler = new stdClass();
  }

  // Set up defaults.
  if (!isset($handler->value)) {
    $handler->value = array();
  }
  if (!isset($handler->operator)) {
    $handler->operator = 'or';
  }
  if ($str == '') {
    return $handler;
  }

  // Determine if the string has 'or' operators (plus signs) or 'and' operators
  // (commas) and split the string accordingly. If we have an 'and' operator,
  // spaces are treated as part of the word being split, but otherwise they are
  // treated the same as a plus sign.
  $or_wildcard = '[^\\s+,]';
  $and_wildcard = '[^+,]';
  if (preg_match("/^({$or_wildcard}+[+ ])+{$or_wildcard}+\$/", $str)) {
    $handler->operator = 'or';
    $handler->value = preg_split('/[+ ]/', $str);
  }
  elseif (preg_match("/^({$and_wildcard}+,)*{$and_wildcard}+\$/", $str)) {
    $handler->operator = 'and';
    $handler->value = explode(',', $str);
  }

  // Keep an 'error' value if invalid strings were given.
  if (!empty($str) && (empty($handler->value) || !is_array($handler->value))) {
    $handler->value = array(
      -1,
    );
    return $handler;
  }

  // Doubly ensure that all values are strings only.
  foreach ($handler->value as $id => $value) {
    $handler->value[$id] = (string) $value;
  }
  return $handler;
}