You are here

function _easy_breadcrumb_normalize_text in Easy Breadcrumb 7.2

Normalizes a text.

E.g., transforms "about-us" to "About Us" or "About us", according to parameters.

Parameters

string $raw_text: Text to be normalized.

Return value

string normalized title.

2 calls to _easy_breadcrumb_normalize_text()
_easy_breadcrumb_obtain_page_title in ./easy_breadcrumb.module
Obtain the title of the page.
_easy_breadcrumb_obtain_segment_title in ./easy_breadcrumb.module
Obtain the title of the given segment.

File

./easy_breadcrumb.module, line 520
The Easy Breadcrumb module provides a block to be embedded in any page.

Code

function _easy_breadcrumb_normalize_text($raw_text) {

  // Filter the raw text against XSS.
  $normalized_text = check_url($raw_text);

  // Transforms '-hello--world_javascript-' on 'hello world javascript'.
  $normalized_text = str_replace(array(
    '-',
    '_',
  ), ' ', $normalized_text);
  $normalized_text = trim($normalized_text);
  $normalized_text = preg_replace('/\\s{2,}/', ' ', $normalized_text);

  // Gets the flag saying the capitalizator mode.
  $capitalizator_mode = variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_MODE, 'ucwords');
  if ($capitalizator_mode === 'ucwords') {

    // Transforms the text 'once a time' to 'Once a Time'.
    // List of words to be ignored by the capitalizator.
    $ignored_words = variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_IGNORED_WORDS, EasyBreadcrumbConstants::defaultIgnoredWords());
    $words_arr = explode(' ', $normalized_text);

    // Transforms the non-ignored words of the segment.
    $words_arr[0] = drupal_ucfirst($words_arr[0]);
    for ($idx_1 = 1, $words_quantity = count($words_arr); $idx_1 < $words_quantity; ++$idx_1) {

      // Transforms this word only if it is not in the list of ignored words.
      if (!isset($ignored_words[$words_arr[$idx_1]])) {
        $words_arr[$idx_1] = drupal_ucfirst($words_arr[$idx_1]);
      }
    }
    $normalized_text = implode(' ', $words_arr);
  }
  elseif ($capitalizator_mode === 'ucall') {

    // Transforms the text 'once a time' to 'ONCE A TIME'.
    $normalized_text = drupal_strtoupper($normalized_text);
  }
  elseif ($capitalizator_mode === 'ucforce') {

    // Transforms the text 'once a time' to 'once a TIME'.
    // List of words to be forced by the capitalizator.
    $forced_words = variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS, '');

    // If case sensitivity is false make all the forced words
    // uncapitalized by default.
    if (!variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS_CASE_SENSITIVITY, FALSE)) {
      $forced_words = array_map('drupal_strtolower', $forced_words);
    }
    $words_arr = explode(' ', $normalized_text);

    // Transforms the non-ignored words of the segment.
    if (variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS_FIRST_LETTER, '')) {
      $words_arr[0] = drupal_ucfirst($words_arr[0]);
    }
    for ($idx_1 = 0, $words_quantity = count($words_arr); $idx_1 < $words_quantity; ++$idx_1) {

      // If case sensitivity is false make the compared word uncapitalized
      // in order to allow the comparison well.
      if (!variable_get(EasyBreadcrumbConstants::DB_VAR_CAPITALIZATOR_FORCED_WORDS_CASE_SENSITIVITY, FALSE)) {
        $selected_word = drupal_strtolower($words_arr[$idx_1]);
      }
      else {
        $selected_word = $words_arr[$idx_1];
      }

      // Transforms this word only if it is in the list of forced words.
      if (in_array($selected_word, $forced_words)) {
        $words_arr[$idx_1] = drupal_strtoupper($selected_word);
      }
    }
    $normalized_text = implode(' ', $words_arr);
  }
  else {

    // Transforms the text 'once a time' to 'Once a time' (ucfirst).
    $normalized_text = drupal_ucfirst($normalized_text);
  }
  return $normalized_text;
}