You are here

function recipe_ingredient_quantity_from_fraction in Recipe 6

Same name and namespace in other branches
  1. 5 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.

1 call to recipe_ingredient_quantity_from_fraction()
recipe_save_ingredients in ./recipe.module
Saves the ingredients of a recipe node to the database.

File

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

Code

function recipe_ingredient_quantity_from_fraction($ingredient_quantity) {

  // Replace a dash separated fraction with a ' ' to normalize the input string.
  $ingredient_quantity = preg_replace('/^(\\d+)[\\-](\\d+)[\\/](\\d+)/', '${1} ${2}/${3}', $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;
}