You are here

function feeds_ex_encoding_autocomplete in Feeds extensible parsers 7

Same name and namespace in other branches
  1. 8 feeds_ex.pages.inc \feeds_ex_encoding_autocomplete()
  2. 7.2 feeds_ex.pages.inc \feeds_ex_encoding_autocomplete()

Autocomplete callback for encodings.

1 string reference to 'feeds_ex_encoding_autocomplete'
feeds_ex_menu in ./feeds_ex.module
Implements hook_menu().

File

./feeds_ex.pages.inc, line 11
Page callbacks for feeds_ex.

Code

function feeds_ex_encoding_autocomplete($string = '') {
  $matches = array();
  if (!strlen($string) || $GLOBALS['multibyte'] != UNICODE_MULTIBYTE) {
    drupal_json_output($matches);
    return;
  }
  $added = array_map('trim', explode(',', $string));
  $string = array_pop($added);
  $lower_added = array_map('drupal_strtolower', $added);

  // Filter out items already added. Do it case insensitively without changing
  // the suggested case.
  $prefix = '';
  $encodings = array();
  foreach (mb_list_encodings() as $suggestion) {
    if (in_array(drupal_strtolower($suggestion), $lower_added)) {
      $prefix .= $suggestion . ', ';
      continue;
    }
    $encodings[] = $suggestion;
  }

  // Find starts with first.
  foreach ($encodings as $delta => $encoding) {
    if (stripos($encoding, $string) !== 0) {
      continue;
    }
    $matches[$prefix . $encoding] = check_plain($encoding);

    // Remove matches so we don't search them again.
    unset($encodings[$delta]);
  }

  // Find contains next.
  foreach ($encodings as $encoding) {
    if (stripos($encoding, $string) !== FALSE) {
      $matches[$prefix . $encoding] = check_plain($encoding);
    }
  }

  // Only send back 10 suggestions.
  $matches = array_slice($matches, 0, 10, TRUE);
  drupal_json_output($matches);
}