/** * Convert text into string suitable for sorting, by moving articles to end of string, etc. * * @param string $ps_text Text to convert to sortable value * @param array $pa_options Options include: * locale = Locale settings to use. If omitted current default locale is used. [Default is current locale] * omitArticle = Omit leading definite and indefinited articles, rather than moving them to the end of the text [Default is true] * * @return string Converted text. If locale cannot be found $ps_text is returned unchanged. */ function caSortableValue($ps_text, $pa_options = null) { global $g_ui_locale; $ps_locale = caGetOption('locale', $pa_options, $g_ui_locale); if (!$ps_locale) { return $ps_text; } $pb_omit_article = caGetOption('omitArticle', $pa_options, true); $o_locale_settings = TimeExpressionParser::getSettingsForLanguage($ps_locale); $vs_display_value = trim(preg_replace('![^\\p{L}0-9 ]+!u', ' ', $ps_text)); // Move articles to end of string $va_definite_articles = $o_locale_settings ? $o_locale_settings->get('definiteArticles') : array(); $va_indefinite_articles = $o_locale_settings ? $o_locale_settings->get('indefiniteArticles') : array(); foreach (array($va_definite_articles, $va_indefinite_articles) as $va_articles) { if (is_array($va_articles)) { foreach ($va_articles as $vs_article) { if (preg_match('!^(' . $vs_article . ')[ ]+!i', $vs_display_value, $va_matches)) { $vs_display_value = trim(str_replace($va_matches[1], '', $vs_display_value) . ($pb_omit_article ? '' : ', ' . $va_matches[1])); break 2; } } } } // Left-pad numbers if (preg_match("![\\d]+!", $vs_display_value, $va_matches)) { for ($i = 0; $i < sizeof($va_matches); $i++) { $vs_padded = str_pad($va_matches[$i], 15, 0, STR_PAD_LEFT); $vs_display_value = str_replace($va_matches[$i], $vs_padded, $vs_display_value); } } return $vs_display_value; }
/** * Convert text into string suitable for sorting, by moving articles to end of string, etc. * * @param string $ps_text Text to convert to sortable value * @param array $pa_options Options include: * locale = Locale settings to use. If omitted current default locale is used. [Default is current locale] * * @return string Converted text. If locale cannot be found $ps_text is returned unchanged. */ function caSortableValue($ps_text, $pa_options = null) { global $g_ui_locale; $ps_locale = caGetOption('locale', $pa_options, $g_ui_locale); if (!$ps_locale) { return $ps_text; } $o_locale_settings = TimeExpressionParser::getSettingsForLanguage($ps_locale); $vs_display_value = trim(preg_replace('![^\\p{L}0-9 ]+!u', ' ', $ps_text)); $va_definite_articles = $o_locale_settings->get('definiteArticles'); $va_indefinite_articles = $o_locale_settings->get('indefiniteArticles'); foreach (array($va_definite_articles, $va_indefinite_articles) as $va_articles) { if (is_array($va_articles)) { foreach ($va_articles as $vs_article) { if (preg_match('!^(' . $vs_article . ')[ ]+!i', $vs_display_value, $va_matches)) { $vs_display_value = trim(str_replace($va_matches[1], '', $vs_display_value) . ', ' . $va_matches[1]); break 2; } } } } return $vs_display_value; }