uTools-Manuals/docs/php/strnatcmp.html
2019-04-28 19:00:34 +08:00

137 lines
7.2 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>使用自然排序算法比较字符串</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.strnatcmp" class="refentry">
<div class="refnamediv">
<h1 class="refname">strnatcmp</h1>
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</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">string</span> <code class="parameter">$str1</code></span>
, <span class="methodparam"><span class="type">string</span> <code class="parameter">$str2</code></span>
) : <span class="type">int</span></div>
<p class="para rdfs-comment">
该函数实现了以人类习惯对数字型字符串进行排序的比较算法,这就是“自然顺序”。注意该比较区分大小写。
</p>
</div>
<div class="refsect1 parameters" id="refsect1-function.strnatcmp-parameters">
<h3 class="title">参数</h3>
<p class="para">
<dl>
<dt>
<code class="parameter">str1</code></dt>
<dd>
<p class="para">
第一个字符串。
</p>
</dd>
<dt>
<code class="parameter">str2</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="para">
与其他字符串比较函数类似,如果 <code class="parameter">str1</code> 小于
<code class="parameter">str2</code> 返回 &lt; 0
如果 <code class="parameter">str1</code>
大于 <code class="parameter">str2</code>
返回 &gt; 0如果两者相等返回 0。
</p>
</div>
<div class="refsect1 examples" id="refsect1-function.strnatcmp-examples">
<h3 class="title">范例</h3>
<p class="para">
下面的例子展示了该算法与计算机常规字符串比较算法( <span class="function"><a href="strcmp.html" class="function">strcmp()</a></span> 所使用的)的区别:
<div class="informalexample">
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$arr1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$arr2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"img12.png"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"img10.png"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"img2.png"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"img1.png"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Standard&nbsp;string&nbsp;comparison\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">,&nbsp;</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 />echo&nbsp;</span><span style="color: #DD0000">"\nNatural&nbsp;order&nbsp;string&nbsp;comparison\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">,&nbsp;</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 /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<p class="para">以上例程会输出:</p>
<div class="example-contents screen">
<div class="cdata"><pre>
Standard string comparison
Array
(
[0] =&gt; img1.png
[1] =&gt; img10.png
[2] =&gt; img12.png
[3] =&gt; img2.png
)
Natural order string comparison
Array
(
[0] =&gt; img1.png
[1] =&gt; img2.png
[2] =&gt; img10.png
[3] =&gt; img12.png
)
</pre></div>
</div>
</div>
更多信息参见Martin Pool 的<a href="http://sourcefrog.net/projects/natsort/" class="link external">&raquo;&nbsp;自然顺序的字符串比较</a>
page.
</p>
</div>
<div class="refsect1 seealso" id="refsect1-function.strnatcmp-seealso">
<h3 class="title">参见</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="function"><a href="preg_match.html" class="function" rel="rdfs-seeAlso">preg_match()</a> - 执行匹配正则表达式</span></li>
<li class="member"><span class="function"><a href="strcasecmp.html" class="function" rel="rdfs-seeAlso">strcasecmp()</a> - 二进制安全比较字符串(不区分大小写)</span></li>
<li class="member"><span class="function"><a href="substr.html" class="function" rel="rdfs-seeAlso">substr()</a> - 返回字符串的子串</span></li>
<li class="member"><span class="function"><a href="stristr.html" class="function" rel="rdfs-seeAlso">stristr()</a> - strstr 函数的忽略大小写版本</span></li>
<li class="member"><span class="function"><a href="strcmp.html" class="function" rel="rdfs-seeAlso">strcmp()</a> - 二进制安全字符串比较</span></li>
<li class="member"><span class="function"><a href="strncmp.html" class="function" rel="rdfs-seeAlso">strncmp()</a> - 二进制安全比较字符串开头的若干个字符</span></li>
<li class="member"><span class="function"><a href="strncasecmp.html" class="function" rel="rdfs-seeAlso">strncasecmp()</a> - 二进制安全比较字符串开头的若干个字符(不区分大小写)</span></li>
<li class="member"><span class="function"><a href="strnatcasecmp.html" class="function" rel="rdfs-seeAlso">strnatcasecmp()</a> - 使用&ldquo;自然顺序&rdquo;算法比较字符串(不区分大小写)</span></li>
<li class="member"><span class="function"><a href="strstr.html" class="function" rel="rdfs-seeAlso">strstr()</a> - 查找字符串的首次出现</span></li>
<li class="member"><span class="function"><a href="natsort.html" class="function" rel="rdfs-seeAlso">natsort()</a> - 用&ldquo;自然排序&rdquo;算法对数组排序</span></li>
<li class="member"><span class="function"><a href="natcasesort.html" class="function" rel="rdfs-seeAlso">natcasesort()</a> - 用&ldquo;自然排序&rdquo;算法对数组进行不区分大小写字母的排序</span></li>
</ul>
</p>
</div>
</div></div></div></body></html>