Browse Source

[svn r21528] FS#306 - The multi-byte string library: Adding new functions for reverse string comparisons - api_strcasercmp(), api_strrcmp(), api_strnatcasercmp(), api_strnatrcmp(). They are handy to be used as callback functions.

Ivan Tcholakov 15 years ago
parent
commit
f83ae8a87f
1 changed files with 51 additions and 3 deletions
  1. 51 3
      main/inc/lib/multibyte_string_functions.lib.php

+ 51 - 3
main/inc/lib/multibyte_string_functions.lib.php

@@ -1033,7 +1033,7 @@ function api_add_pcre_unicode_modifier($pcre, $encoding = null) {
  * @param string $string2				The second string.
  * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
- * @return int							Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal. 
+ * @return int							Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal.
  * This function is aimed at replacing the function strcasecmp() for human-language strings.
  * @link http://php.net/manual/en/function.strcasecmp
  */
@@ -1041,13 +1041,25 @@ function api_strcasecmp($string1, $string2, $language = null, $encoding = null)
 	return api_strcmp(api_strtolower($string1, $encoding), api_strtolower($string2, $encoding), $language, $encoding);
 }
 
+/**
+ * Performs reverse string comparison, case insensitive, language sensitive, with extended multibyte support.
+ * @param string $string1				The first string.
+ * @param string $string2				The second string.
+ * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
+ * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
+ * @return int							Returns < 0 if $string2 is less than $string1; > 0 if $string2 is greater than $string1; and 0 if the strings are equal.
+ */
+function api_strcasercmp($string1, $string2, $language = null, $encoding = null) {
+	return api_strcasecmp($string2, $string1, $language, $encoding);
+}
+
 /**
  * Performs string comparison, case sensitive, language sensitive, with extended multibyte support.
  * @param string $string1				The first string.
  * @param string $string2				The second string.
  * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
- * @return int							Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal. 
+ * @return int							Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal.
  * This function is aimed at replacing the function strcmp() for human-language strings.
  * @link http://php.net/manual/en/function.strcmp.php
  * @link http://php.net/manual/en/collator.compare.php
@@ -1063,13 +1075,25 @@ function api_strcmp($string1, $string2, $language = null, $encoding = null) {
 	return strcmp($string1, $string2);
 }
 
+/**
+ * Performs reverse string comparison, case sensitive, language sensitive, with extended multibyte support.
+ * @param string $string1				The first string.
+ * @param string $string2				The second string.
+ * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
+ * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
+ * @return int							Returns < 0 if $string2 is less than $string1; > 0 if $string2 is greater than $string1; and 0 if the strings are equal.
+ */
+function api_strrcmp($string1, $string2, $language = null, $encoding = null) {
+	return api_strcmp($string2, $string1, $language, $encoding);
+}
+
 /**
  * Performs string comparison in so called "natural order", case insensitive, language sensitive, with extended multibyte support.
  * @param string $string1				The first string.
  * @param string $string2				The second string.
  * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
- * @return int							Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal. 
+ * @return int							Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal.
  * This function is aimed at replacing the function strnatcasecmp() for human-language strings.
  * @link http://php.net/manual/en/function.strnatcasecmp
  */
@@ -1077,6 +1101,18 @@ function api_strnatcasecmp($string1, $string2, $language = null, $encoding = nul
 	return api_strnatcmp(api_strtolower($string1, $encoding), api_strtolower($string2, $encoding), $language, $encoding);
 }
 
+/**
+ * Performs reverse string comparison in so called "natural order", case insensitive, language sensitive, with extended multibyte support.
+ * @param string $string1				The first string.
+ * @param string $string2				The second string.
+ * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
+ * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
+ * @return int							Returns < 0 if $string2 is less than $string1; > 0 if $string2 is greater than $string1; and 0 if the strings are equal.
+ */
+function api_strnatcasercmp($string1, $string2, $language = null, $encoding = null) {
+	return api_strnatcmp(api_strtolower($string2, $encoding), api_strtolower($string1, $encoding), $language, $encoding);
+}
+
 /**
  * Performs string comparison in so called "natural order", case sensitive, language sensitive, with extended multibyte support.
  * @param string $string1				The first string.
@@ -1099,6 +1135,18 @@ function api_strnatcmp($string1, $string2, $language = null, $encoding = null) {
 	return strnatcmp($string1, $string2);
 }
 
+/**
+ * Performs reverse string comparison in so called "natural order", case sensitive, language sensitive, with extended multibyte support.
+ * @param string $string1				The first string.
+ * @param string $string2				The second string.
+ * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
+ * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
+ * @return int							Returns < 0 if $string2 is less than $string1; > 0 if $string2 is greater than $string1; and 0 if the strings are equal. 
+ */
+function api_strnatrcmp($string1, $string2, $language = null, $encoding = null) {
+	return api_strnatcmp($string2, $string1, $language, $encoding);
+}
+
 /**
  * Checks if a value exists in an array, a case insensitive version of in_array() function with extended multibyte support.
  * @param mixed $needle					The searched value. If needle is a string, the comparison is done in a case-insensitive manner.