<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.strings.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ru',
  ),
  'this' => 
  array (
    0 => 'function.strnatcmp.php',
    1 => 'strnatcmp',
    2 => 'Сравнивает строки алгоритмом естественного упорядочивания',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'Функции для работы со строками',
  ),
  'prev' => 
  array (
    0 => 'function.strnatcasecmp.php',
    1 => 'strnatcasecmp',
  ),
  'next' => 
  array (
    0 => 'function.strncasecmp.php',
    1 => 'strncasecmp',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ru',
    'path' => 'reference/strings/functions/strnatcmp.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.strnatcmp" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">strnatcmp</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">strnatcmp</span> &mdash; <span class="dc-title">Сравнивает строки алгоритмом естественного упорядочивания</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.strnatcmp-description">
  <h3 class="title">Описание</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>strnatcmp</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string1</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string2</code></span>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <p class="para rdfs-comment">
   Функция реализует алгоритм сравнения, который упорядочивает
   буквенно-цифровые строки подобно тому, как это сделал бы
   человек, такой алгоритм называется «естественное упорядочивание» (англ. natural ordering).
   Функция учитывает регистр при сравнении строк.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.strnatcmp-parameters">
  <h3 class="title">Список параметров</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">string1</code></dt>
     <dd>
      <p class="para">
       Первая строка.
      </p>
     </dd>
    
    
     <dt><code class="parameter">string2</code></dt>
     <dd>
      <p class="para">
       Вторая строка.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.strnatcmp-returnvalues">
  <h3 class="title">Возвращаемые значения</h3>
  
  <p class="simpara">
   Функция возвращает значение меньше 0, если строка <code class="parameter">string1</code>
   меньше строки <code class="parameter">string2</code>; значение больше
   0, если строка <code class="parameter">string1</code> больше строки
   <code class="parameter">string2</code>, и <code class="literal">0</code>, если строки
   равны.
   Из значения возврата невозможно надёжно вывести конкретное значение,
   кроме знака числа.
  </p>

 </div>


 <div class="refsect1 changelog" id="refsect1-function.strnatcmp-changelog">
  <h3 class="title">Список изменений</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Версия</th>
      <th>Описание</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
 <td>8.2.0</td>
 <td>
  Функция больше не гарантирует возврат разницы
  <code class="code">strlen($string1) - strlen($string2)</code>, когда длины строк
  не равны, но теперь вместо этого иногда возвращает <code class="literal">-1</code>
  или <code class="literal">1</code>.
 </td>
</tr>


    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.strnatcmp-examples">
  <h3 class="title">Примеры</h3>
  <p class="para">
   Следующий параграф приводит пример, который показывает отличие этого алгоритма от стандартных
   функций сравнения наподобие функции <span class="function"><a href="function.strcmp.php" class="function">strcmp()</a></span>:
   <div class="example" id="example-1">
    <p><strong>Пример #1 Пример сравнения строк функцией <span class="function"><a href="function.strcmp.php" class="function">strcmp()</a></span></strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$arr1 </span><span style="color: #007700">= </span><span style="color: #0000BB">$arr2 </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"img12.png"</span><span style="color: #007700">, </span><span style="color: #DD0000">"img10.png"</span><span style="color: #007700">, </span><span style="color: #DD0000">"img2.png"</span><span style="color: #007700">, </span><span style="color: #DD0000">"img1.png"</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Стандартный алгоритм сравнения\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">usort</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">, </span><span style="color: #DD0000">"strcmp"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"\nАлгоритм \"natural order\"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">usort</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr2</span><span style="color: #007700">, </span><span style="color: #DD0000">"strnatcmp"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    
<div class="example-contents"><p>
 Результат выполнения приведённого примера:
</p></div>

    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Стандартный алгоритм сравнения
Array
(
    [0] =&gt; img1.png
    [1] =&gt; img10.png
    [2] =&gt; img12.png
    [3] =&gt; img2.png
)

Алгоритм &quot;natural order&quot;
Array
(
    [0] =&gt; img1.png
    [1] =&gt; img2.png
    [2] =&gt; img10.png
    [3] =&gt; img12.png
)</pre>
</div>
    </div>
   </div>
   Подробнее об алгоритме рассказывает страница
   «<a href="https://github.com/sourcefrog/natsort" class="link external">&raquo;&nbsp;Сравнение строк в естественном порядке</a>».
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.strnatcmp-seealso">
  <h3 class="title">Смотрите также</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.preg-match.php" class="function" rel="rdfs-seeAlso">preg_match()</a> - Выполняет проверку на соответствие регулярному выражению</span></li>
    <li><span class="function"><a href="function.strcasecmp.php" class="function" rel="rdfs-seeAlso">strcasecmp()</a> - Сравнивает строки без учёта регистра в бинарно безопасном режиме</span></li>
    <li><span class="function"><a href="function.substr.php" class="function" rel="rdfs-seeAlso">substr()</a> - Возвращает подстроку</span></li>
    <li><span class="function"><a href="function.stristr.php" class="function" rel="rdfs-seeAlso">stristr()</a> - Регистронезависимый вариант функции strstr</span></li>
    <li><span class="function"><a href="function.strcmp.php" class="function" rel="rdfs-seeAlso">strcmp()</a> - Сравнивает строки в бинарно-безопасном режиме: как последовательности байтов</span></li>
    <li><span class="function"><a href="function.strncmp.php" class="function" rel="rdfs-seeAlso">strncmp()</a> - Сравнивает первые n символов строк в бинарно безопасном режиме</span></li>
    <li><span class="function"><a href="function.strncasecmp.php" class="function" rel="rdfs-seeAlso">strncasecmp()</a> - Сравнивает первые n символов строк без учёта регистра в бинарно-безопасном режиме</span></li>
    <li><span class="function"><a href="function.strnatcasecmp.php" class="function" rel="rdfs-seeAlso">strnatcasecmp()</a> - Сравнивает строки без учёта регистра по алгоритму natural order</span></li>
    <li><span class="function"><a href="function.strstr.php" class="function" rel="rdfs-seeAlso">strstr()</a> - Находит первое вхождение подстроки</span></li>
    <li><span class="function"><a href="function.natsort.php" class="function" rel="rdfs-seeAlso">natsort()</a> - Сортирует массив алгоритмом &laquo;естественного упорядочивания&raquo;</span></li>
    <li><span class="function"><a href="function.natcasesort.php" class="function" rel="rdfs-seeAlso">natcasesort()</a> - Сортирует массив алгоритмом естественной сортировки (natural order) без учёта регистра символов</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>