class Collator (View source)

Constants

DEFAULT_VALUE

PRIMARY

SECONDARY

TERTIARY

DEFAULT_STRENGTH

QUATERNARY

IDENTICAL

OFF

ON

SHIFTED

NON_IGNORABLE

LOWER_FIRST

UPPER_FIRST

FRENCH_COLLATION

Sort strings with different accents from the back of the string. This attribute is automatically set to On for the French locales and a few others. Users normally would not need to explicitly set this attribute. There is a string comparison performance cost when it is set On, but sort key length is unaffected. Possible values are: Collator::ON Collator::OFF(default) Collator::DEFAULT_VALUE

FRENCH_COLLATION rules

F=OFF cote < coté < côte < côté F=ON cote < côte < coté < côté

ALTERNATE_HANDLING

The Alternate attribute is used to control the handling of the so called variable characters in the UCA: whitespace, punctuation and symbols. If Alternate is set to NonIgnorable (N), then differences among these characters are of the same importance as differences among letters. If Alternate is set to Shifted (S), then these characters are of only minor importance. The Shifted value is often used in combination with Strength set to Quaternary. In such a case, whitespace, punctuation, and symbols are considered when comparing strings, but only if all other aspects of the strings (base letters, accents, and case) are identical. If Alternate is not set to Shifted, then there is no difference between a Strength of 3 and a Strength of 4. For more information and examples, see Variable_Weighting in the UCA.

The reason the Alternate values are not simply On and Off is that additional Alternate values may be added in the future. The UCA option Blanked is expressed with Strength set to 3, and Alternate set to Shifted. The default for most locales is NonIgnorable. If Shifted is selected, it may be slower if there are many strings that are the same except for punctuation; sort key length will not be affected unless the strength level is also increased.

Possible values are: Collator::NON_IGNORABLE(default) Collator::SHIFTED Collator::DEFAULT_VALUE

ALTERNATE_HANDLING rules

S=3, A=N di Silva < Di Silva < diSilva < U.S.A. < USA S=3, A=S di Silva = diSilva < Di Silva < U.S.A. = USA S=4, A=S di Silva < diSilva < Di Silva < U.S.A. < USA

CASE_FIRST

The Case_First attribute is used to control whether uppercase letters come before lowercase letters or vice versa, in the absence of other differences in the strings. The possible values are Uppercase_First (U) and Lowercase_First (L), plus the standard Default and Off.

There is almost no difference between the Off and Lowercase_First options in terms of results, so typically users will not use Lowercase_First: only Off or Uppercase_First. (People interested in the detailed differences between X and L should consult the Collation Customization). Specifying either L or U won't affect string comparison performance, but will affect the sort key length.

Possible values are: Collator::OFF(default) Collator::LOWER_FIRST Collator::UPPER_FIRST Collator:DEFAULT

CASE_FIRST rules

C=X or C=L "china" < "China" < "denmark" < "Denmark" C=U "China" < "china" < "Denmark" < "denmark"

CASE_LEVEL

The Case_Level attribute is used when ignoring accents but not case. In such a situation, set Strength to be Primary, and Case_Level to be On.

In most locales, this setting is Off by default. There is a small string comparison performance and sort key impact if this attribute is set to be On.

Possible values are: Collator::OFF(default) Collator::ON Collator::DEFAULT_VALUE

CASE_LEVEL rules

S=1, E=X role = Role = rôle S=1, E=O role = rôle < Role

NORMALIZATION_MODE

The Normalization setting determines whether text is thoroughly normalized or not in comparison. Even if the setting is off (which is the default for many locales), text as represented in common usage will compare correctly (for details, see UTN #5). Only if the accent marks are in noncanonical order will there be a problem. If the setting is On, then the best results are guaranteed for all possible text input.

There is a medium string comparison performance cost if this attribute is On, depending on the frequency of sequences that require normalization. There is no significant effect on sort key length. If the input text is known to be in NFD or NFKD normalization forms, there is no need to enable this Normalization option.

Possible values are: Collator::OFF(default) Collator::ON Collator::DEFAULT_VALUE

STRENGTH

The ICU Collation Service supports many levels of comparison (named "Levels", but also known as "Strengths"). Having these categories enables ICU to sort strings precisely according to local conventions.

However, by allowing the levels to be selectively employed, searching for a string in text can be performed with various matching conditions. For more detailed information, see collator_set_strength chapter.

Possible values are: Collator::PRIMARY Collator::SECONDARY Collator::TERTIARY(default) Collator::QUATERNARY Collator::IDENTICAL Collator::DEFAULT_VALUE

HIRAGANA_QUATERNARY_MODE

Compatibility with JIS x 4061 requires the introduction of an additional level to distinguish Hiragana and Katakana characters. If compatibility with that standard is required, then this attribute should be set On, and the strength set to Quaternary. This will affect sort key length and string comparison string comparison performance.

Possible values are: Collator::OFF(default) Collator::ON Collator::DEFAULT_VALUE

NUMERIC_COLLATION

When turned on, this attribute generates a collation key for the numeric value of substrings of digits. This is a way to get '100' to sort AFTER '2'.

Possible values are: Collator::OFF(default) Collator::ON Collator::DEFAULT_VALUE

SORT_REGULAR

SORT_STRING

SORT_NUMERIC

Methods

__construct(string $locale)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Create a collator

static Collator|null
create(string $locale)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Create a collator

int|false
compare(string $string1, string $string2)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Compare two Unicode strings

bool
sort(array $array, int $flags = 0)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Sort array using specified collator

bool
sortWithSortKeys(array $array, $flags = [])

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Sort array using specified collator and sort keys

bool
asort(array $array, int $flags = 0)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Sort array maintaining index association

int|false
getAttribute(int $attribute)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get collation attribute value

bool
setAttribute(int $attribute, int $value)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Set collation attribute

int
getStrength()

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get current collation strength

bool
setStrength(int $strength)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Set collation strength

int|false
getErrorCode()

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get collator's last error code

string|false
getLocale(int $type)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get the locale name of the collator

string|false
getErrorMessage()

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get text for collator's last error code

string|false
getSortKey(string $string, $arg2)

(PHP 5 >= 5.3.2, PECL intl >= 1.0.3)
Get sorting key for a string

Details

__construct(string $locale)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Create a collator

Parameters

string $locale

static Collator|null create(string $locale)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Create a collator

Parameters

string $locale

The locale containing the required collation rules. Special values for locales can be passed in - if null is passed for the locale, the default locale collation rules will be used. If empty string ("") or "root" are passed, UCA rules will be used.

Return Value

Collator|null

Return new instance of Collator object, or NULL on error.

int|false compare(string $string1, string $string2)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Compare two Unicode strings

Parameters

string $string1

The first string to compare.

string $string2

The second string to compare.

Return Value

int|false

Return comparison result:

1 if str1 is greater than str2 ;

0 if str1 is equal to str2;

-1 if str1 is less than str2 .

On error boolean FALSE is returned.

bool sort(array $array, int $flags = 0)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Sort array using specified collator

Parameters

array $array

Array of strings to sort.

int $flags

[optional]

Optional sorting type, one of the following:

Collator::SORT_REGULAR - compare items normally (don't change types)

Return Value

bool

TRUE on success or FALSE on failure.

bool sortWithSortKeys(array $array, $flags = [])

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Sort array using specified collator and sort keys

Parameters

array $array

Array of strings to sort

$flags

Return Value

bool

TRUE on success or FALSE on failure.

bool asort(array $array, int $flags = 0)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Sort array maintaining index association

Parameters

array $array

Array of strings to sort.

int $flags

[optional]

Optional sorting type, one of the following: Collator::SORT_REGULAR

  • compare items normally (don't change types)

Return Value

bool

TRUE on success or FALSE on failure.

int|false getAttribute(int $attribute)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get collation attribute value

Parameters

int $attribute

Attribute to get value for.

Return Value

int|false

Attribute value, or boolean FALSE on error.

bool setAttribute(int $attribute, int $value)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Set collation attribute

Parameters

int $attribute Attribute.
int $value

Attribute value.

Return Value

bool

TRUE on success or FALSE on failure.

int getStrength()

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get current collation strength

Return Value

int

current collation strength, or boolean FALSE on error.

bool setStrength(int $strength)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Set collation strength

Parameters

int $strength

Strength to set.

Possible values are: Collator::PRIMARY

Return Value

bool

TRUE on success or FALSE on failure.

int|false getErrorCode()

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get collator's last error code

Return Value

int|false

Error code returned by the last Collator API function call.

string|false getLocale(int $type)

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get the locale name of the collator

Parameters

int $type

You can choose between valid and actual locale ( Locale::VALID_LOCALE and Locale::ACTUAL_LOCALE, respectively).

Return Value

string|false

Real locale name from which the collation data comes. If the collator was instantiated from rules or an error occurred, returns boolean FALSE.

string|false getErrorMessage()

(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get text for collator's last error code

Return Value

string|false

Description of an error occurred in the last Collator API function call.

string|false getSortKey(string $string, $arg2)

(PHP 5 >= 5.3.2, PECL intl >= 1.0.3)
Get sorting key for a string

Parameters

string $string

The string to produce the key from.

$arg2

Return Value

string|false

the collation key for the string. Collation keys can be compared directly instead of strings.