1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="collator.getsortkey" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>Collator::getSortKey</refname>
<refname>collator_get_sort_key</refname>
<refpurpose>Get sorting key for a string</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<para>
&style.oop;
</para>
<methodsynopsis role="Collator">
<modifier>public</modifier> <type class="union"><type>string</type><type>false</type></type><methodname>Collator::getSortKey</methodname>
<methodparam><type>string</type><parameter>string</parameter></methodparam>
</methodsynopsis>
<para>
&style.procedural;
</para>
<methodsynopsis>
<type class="union"><type>string</type><type>false</type></type><methodname>collator_get_sort_key</methodname>
<methodparam><type>Collator</type><parameter>object</parameter></methodparam>
<methodparam><type>string</type><parameter>string</parameter></methodparam>
</methodsynopsis>
<para>
Return collation key for a string.
Collation keys can be compared directly instead of strings, though are implementation
specific and may change between ICU library versions. Sort keys are generally only
useful in databases or other circumstances where function calls are extremely expensive.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>object</parameter></term>
<listitem>
<para>
<classname>Collator</classname> object.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>string</parameter></term>
<listitem>
<para>
The string to produce the key from.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>Returns the collation key for the string, &return.falseforfailure;.</para>
&return.falseproblem;
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>collator_get_sort_key</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
$s1 = 'Hello';
$coll = collator_create('en_US');
$res = collator_get_sort_key($coll, $s1);
echo bin2hex($res);
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
3832404046010901dc08
</screen>
</example>
</para>
<para>
<example>
<title><function>Collator::getSortKey</function> example with <function>usort</function></title>
<programlisting role="php">
<![CDATA[
<?php
$data = [
[ 'name' => '🇳🇱 Derick Rethans', 'linked_account' => 'https://phpc.social/users/derickr' ],
[ 'name' => 'Elephpant', 'linked_account' => 'https://phpc.social/phpc' ],
[ 'name' => '🇫🇷 Marcus Bointon', 'linked_account' => 'https://phpc.social/users/Synchro' ],
];
/* Create the collator */
$col = new Collator('en');
/* Sort upper-case letters before lower-case letters */
$col->setAttribute(Collator::CASE_FIRST, Collator::UPPER_FIRST);
/* Use a user-defined function with sort, that strips out the emojis */
*/
usort(
$data,
function($a, $b) use ($col) {
/* Remove the character class 'S' (the Symbols), and remove whitespace
* (with trim) */
$aName = trim(preg_replace('/\p{S}+/u', '', $a['name']));
$bName = trim(preg_replace('/\p{S}+/u', '', $b['name']));
/* Create the sort key */
$aKey = $col->getSortKey($aName);
$bKey = $col->getSortKey($bName);
/* Use the sort key to signal which element sorts first */
return $aKey <=> $bKey;
}
);
var_dump($data);
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
array(3) {
[0] =>
array(2) {
'name' =>
string(25) "🇳🇱 Derick Rethans"
'linked_account' =>
string(33) "https://phpc.social/users/derickr"
}
[1] =>
array(2) {
'name' =>
string(9) "Elephpant"
'linked_account' =>
string(24) "https://phpc.social/phpc"
}
[2] =>
array(2) {
'name' =>
string(25) "🇫🇷 Marcus Bointon"
'linked_account' =>
string(33) "https://phpc.social/users/Synchro"
}
}
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>collator_sort</function></member>
<member><function>collator_sort_with_sort_keys</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|