You are here

function recipe_parse_ingredient_string in Recipe 5

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

Converts an ingredients name string to an ingredient object

1 call to recipe_parse_ingredient_string()
recipe_validate in ./recipe.module
Implementation of hook_validate().

File

./recipe.module, line 958
recipe.module - share recipes for drupal 5.x

Code

function recipe_parse_ingredient_string($ingredient_string) {
  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;
  }
}