1.20.x utility.inc | views_break_phrase_string($str, &$handler = NULL) |
Break x,y,z and x+y+z into an array. Works for strings.
Parameters
$str: The string to parse.
$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.
File
- modules/
views/ includes/ utility.inc, line 144 - Utility functions for assembling Views queries.
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;
}