You are here

function recipe_unit_fuzzymatch in Recipe 7

Same name and namespace in other branches
  1. 6 recipe.module \recipe_unit_fuzzymatch()
  2. 7.2 recipe.admin.inc \recipe_unit_fuzzymatch()

Returns a best-guess matched unit key for a unit of measure.

Used by the various import plugins.

Parameters

string $recipe_name_or_abbrev: A unit of measure abbreviation or a unit name.

Return value

A recipe unit key as from recipe_get_units or FALSE if no match.

3 calls to recipe_unit_fuzzymatch()
recipe_import_element_end in includes/recipe_recipeML.module
Call-back function used by the XML parser.
recipe_mastercook4_import_single in includes/recipe_mastercook4.module
recipe_plaintext_import in includes/recipe_plaintext.module
Parsing instance for plain text recipes

File

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

Code

function recipe_unit_fuzzymatch($unit_name_or_abbrev) {
  $units = recipe_get_units();

  // Empty strings should use the default non-printing 'unit'.
  if (empty($unit_name_or_abbrev)) {
    $unit_name_or_abbrev = 'unit';
  }

  // First pass unit case must match exactly( T=Tbsp, t=tsp ).
  foreach ($units as $unit_key => $u) {
    $pats = array();

    // Add name pattern.
    $pats[] = '^' . $u['name'] . 's{0,1}$';

    // Add plural name pattern.
    $pats[] = '^' . $u['plural'] . 's{0,1}$';

    // Add abbreviation pattern.
    $pats[] = '^' . $u['abbreviation'] . 's{0,1}\\.{0,1}$';
    foreach ($u['aliases'] as $alias) {
      $pats[] = '^' . trim($alias) . 's{0,1}\\.{0,1}$';
    }
    $search_pat = implode('|', $pats);
    if (preg_match("/{$search_pat}/", $unit_name_or_abbrev)) {
      return $unit_key;
    }
  }

  // Second pass unit case doesn't matter.
  foreach ($units as $unit_key => $u) {
    $pats = array();

    // Add name pattern.
    $pats[] = '^' . $u['name'] . 's{0,1}$';

    // Add plural name pattern.
    $pats[] = '^' . $u['plural'] . 's{0,1}$';

    // Add abbreviation pattern.
    $pats[] = '^' . $u['abbreviation'] . 's{0,1}\\.{0,1}$';
    foreach ($u['aliases'] as $alias) {
      $pats[] = '^' . trim($alias) . 's{0,1}\\.{0,1}$';
    }
    $search_pat = implode('|', $pats);
    if (preg_match("/{$search_pat}/i", $unit_name_or_abbrev)) {
      return $unit_key;
    }
  }
  return FALSE;
}