You are here

function name_clean_name_component in Name Field 7

Checks that the name matches a reasonable format before saving it to the database.

The valid list so far is:

xxx > Xxx d'xxx > D'Xxx l'xxx > L'Xxx m'xxx > M'Xxx o'xxx > O'Xxx de|del|des|di|du|la|le|van|von xxx > De Xxx de la xxx > De la Xxx van|von de|den|der xxx > Van Xxx st|st.|staint xxx > Staint Xxx van der xxx > Van der Xxx mc|mac xxx > McXxx

Ignored list includes:

xxx xxx etc

Configurable options:

xxx-xxx x

Note that this is an API change from Name Field 7.x-1.4 to 7.x-1.5 that was required after implementing theme_name_component(). The previous version of this function was name_process_name_component().

File

./name.module, line 1740
Defines an API for displaying and inputing names.

Code

function name_clean_name_component($name, $data_key = 'user_data', $letters = 'a-z') {
  $name = trim($name);
  $settings = namedb_settings();
  $min = intval($settings[$data_key]['minimum_length']);
  if ($min <= 0) {
    $min = 1;
  }
  if (empty($name) || strpos($name, '-') || drupal_strlen($name) < $min) {
    return FALSE;
  }
  if (preg_match('/^de la ([' . $letters . ']{' . $min . ',})$/i', $name, $match)) {
    return 'De la ' . drupal_ucfirst($match[1]);
  }
  elseif (preg_match('/^(van|von) (de|den|der) ([' . $letters . ']{' . $min . ',})$/i', $name, $match)) {
    return drupal_ucfirst(drupal_strtolower($match[1])) . ' ' . drupal_strtolower($match[2]) . ' ' . drupal_ucfirst($match[3]);
  }
  elseif (preg_match('/^(st\\.|st|staint) ([' . $letters . ']{' . $min . ',})$/i', $name, $match)) {
    return 'Staint ' . drupal_ucfirst($match[2]);
  }
  elseif (preg_match('/^(d|l|m|o) *\' *([' . $letters . ']{' . $min . ',})$/i', $name, $match)) {
    return drupal_strtoupper($match[1]) . "'" . drupal_ucfirst($match[2]);
  }
  elseif (preg_match('/^(de|del|des|di|du|la|le|van|von) ([' . $letters . ']{' . $min . ',})$/i', $name, $match)) {
    return drupal_ucfirst(drupal_strtolower($match[1])) . ' ' . drupal_ucfirst($match[2]);
  }
  elseif (preg_match('/^(mc|mac) ([' . $letters . ']{' . $min . ',})$/i', $name, $match)) {
    return drupal_ucfirst(drupal_strtolower($match[1])) . drupal_ucfirst($match[2]);
  }
  elseif (preg_match('/^([' . $letters . ']{' . $min . ',}) de ([' . $letters . ']{' . $min . ',})$/i', $name, $match)) {
    return drupal_ucfirst(drupal_strtolower($match[1])) . ' de ' . drupal_ucfirst($match[2]);
  }
  elseif (preg_match('/^[' . $letters . ']{' . $min . ',}$/i', $name)) {
    return drupal_ucfirst($name);
  }
  if ($settings[$data_key]['hyphenated']) {
    if (preg_match('/^([' . $letters . ']{' . $min . ',}) *\\- *([' . $letters . ']{' . $min . ',})$/i', $name)) {
      return drupal_ucfirst($match[1]) . '-' . drupal_ucfirst($match[2]);
    }
  }
  return FALSE;
}