You are here

function recipe_parse_ingredient_string in Recipe 6

Same name and namespace in other branches
  1. 5 recipe.module \recipe_parse_ingredient_string()

Converts an ingredients name string to an ingredient object.

File

./recipe.module, line 1222
recipe.module - share recipes

Code

function recipe_parse_ingredient_string($ingredient_string) {
  $ingredient = array();
  if (preg_match('#([0-9.]+(?:\\s?\\d*/\\d*)?\\s?)?(?:([a-zA-Z.]*)\\s)?(.*)#', trim($ingredient_string), $matches)) {
    $ingredient['name'] = $matches[3];
    $ingredient['quantity'] = trim($matches[1]);
    if ($ingredient['quantity'] == 0) {
      $ingredient['quantity'] = 0;
    }
    $t_unit = $matches[2];
    $unit = recipe_unit_from_name($t_unit);
    if ($unit) {
      $ingredient['unit_id'] = $unit->id;
      $ingredient['abbreviation'] = $unit->abbreviation;
    }
    else {
      $ingredient['unit_id'] = 29;
      $ingredient['abbreviation'] = '';
      $ingredient['name'] = $t_unit . ' ' . $ingredient['name'];
    }
    $ingredient['name'] = trim($ingredient['name']);
    return $ingredient;
  }
  else {
    return FALSE;
  }
}