You are here

function recipe_ingredient_quantity_from_fraction in Recipe 5

Same name and namespace in other branches
  1. 6 recipe.module \recipe_ingredient_quantity_from_fraction()
  2. 7.2 recipe.module \recipe_ingredient_quantity_from_fraction()
  3. 7 recipe.module \recipe_ingredient_quantity_from_fraction()

Converts an ingredient's quantity from fractions to decimal

2 calls to recipe_ingredient_quantity_from_fraction()
recipe_import_from_46 in ./recipe.module
recipe_save_ingredients in ./recipe.module
Saves the changed ingredients of a recipe node to the database (by comparing the old and new ingredients first)

File

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

Code

function recipe_ingredient_quantity_from_fraction($ingredient_quantity) {
  if ($pos_slash = strpos($ingredient_quantity, '/')) {
    $pos_space = strpos($ingredient_quantity, ' ');

    // can't trust $pos_space to be a zero value if there is no space
    // so set it explicitly
    if ($pos_space === false) {
      $pos_space = 0;
    }
    $whole = substr($ingredient_quantity, 0, $pos_space);
    $numerator = substr($ingredient_quantity, $pos_space, $pos_slash);
    $denominator = substr($ingredient_quantity, $pos_slash + 1);
    $ingredient_quantity = $whole + $numerator / $denominator;
  }
  return $ingredient_quantity;
}