php-doc-en/reference/strings/functions/strnatcmp.xml
2020-12-05 14:30:42 +00:00

146 lines
3.8 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://docbook.org/ns/docbook" xml:id="function.strnatcmp">
<refnamediv>
<refname>strnatcmp</refname>
<refpurpose>String comparisons using a "natural order" algorithm</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>int</type><methodname>strnatcmp</methodname>
<methodparam><type>string</type><parameter>string1</parameter></methodparam>
<methodparam><type>string</type><parameter>string2</parameter></methodparam>
</methodsynopsis>
<para>
This function implements a comparison algorithm that orders
alphanumeric strings in the way a human being would, this is
described as a "natural ordering".
Note that this comparison is case sensitive.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>string1</parameter></term>
<listitem>
<para>
The first string.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>string2</parameter></term>
<listitem>
<para>
The second string.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Similar to other string comparison functions, this one returns &lt; 0 if
<parameter>string1</parameter> is less than <parameter>string2</parameter>; &gt;
0 if <parameter>string1</parameter> is greater than
<parameter>string2</parameter>, and 0 if they are equal.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
An example of the difference between this algorithm and the regular
computer string sorting algorithms (used in <function>strcmp</function>)
can be seen below:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$arr1 = $arr2 = array("img12.png", "img10.png", "img2.png", "img1.png");
echo "Standard string comparison\n";
usort($arr1, "strcmp");
print_r($arr1);
echo "\nNatural order string comparison\n";
usort($arr2, "strnatcmp");
print_r($arr2);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Standard string comparison
Array
(
[0] => img1.png
[1] => img10.png
[2] => img12.png
[3] => img2.png
)
Natural order string comparison
Array
(
[0] => img1.png
[1] => img2.png
[2] => img10.png
[3] => img12.png
)
]]>
</screen>
</informalexample>
For more information see: Martin Pool's <link
xlink:href="&url.strnatcmp;">Natural Order String Comparison</link>
page.
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>preg_match</function></member>
<member><function>strcasecmp</function></member>
<member><function>substr</function></member>
<member><function>stristr</function></member>
<member><function>strcmp</function></member>
<member><function>strncmp</function></member>
<member><function>strncasecmp</function></member>
<member><function>strnatcasecmp</function></member>
<member><function>strstr</function></member>
<member><function>natsort</function></member>
<member><function>natcasesort</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
-->