You are here

function recipe_ingredient_quantity_from_fraction in Recipe 7

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

Converts an ingredient's quantity from fraction to decimal.

4 calls to recipe_ingredient_quantity_from_fraction()
RecipeUnitTest::testIngredientQuantityConversion in src/Tests/RecipeUnitTest.php
Test ingredient quantity conversion functions.
recipe_mastercook4_import_single in includes/recipe_mastercook4.module
recipe_plaintext_import in includes/recipe_plaintext.module
Parsing instance for plain text recipes
recipe_save_ingredients in ./recipe.module
Saves the ingredients of a recipe node to the database.

File

./recipe.module, line 1219
Contains functions for Recipe node CRUD and display.

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 = (int) substr($ingredient_quantity, 0, $pos_space);
    $numerator = (int) substr($ingredient_quantity, $pos_space, $pos_slash);
    $denominator = (int) substr($ingredient_quantity, $pos_slash + 1);
    $ingredient_quantity = $whole + $numerator / $denominator;
  }
  return $ingredient_quantity;
}