You are here

function recipe_ingredient_quantity_from_fraction in Recipe 7.2

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 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_field_presave in ./recipe.module
Implements hook_field_presave().
recipe_mastercook4_import_single in modules/recipe_mastercook4.module
recipe_plaintext_import in modules/recipe_plaintext.module
Parsing instance for plain text recipes

File

./recipe.module, line 1114
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;
}