PHP 字符串 strnatcmp() 函數
strnatcmp() 函數是 PHP 的內置字符串函數。愛掏網 - it200.com它用于使用自然算法比較字符串。愛掏網 - it200.com
int strnatcmp ( string str1 , stringstr2 )
Parameter | Description | Required/Optional |
---|---|---|
string1 | required | |
string2 | required |
這個函數strnatcmp():
- 返回:<0如果str1小于str2;
- 返回:>0如果str1大于str2;
- 返回:0如果它們相等。愛掏網 - it200.com
注意:它是區分大小寫的。愛掏網 - it200.com
示例1
<?php
str1="2Hello world!";str2="10Hello world!";
echo "By Using strnatcmp()Function:".strnatcmp(str1,str2);
// It returns -1.
?>
輸出:
By Using strnatcmp()Function:-1
示例2
<?php
str1="10Hello world!";str2="2Hello world!";
echo "By Using strnatcmp()Function:".strnatcmp(str1,str2);
// It returns 1.
?>
輸出:
By Using strnatcmp()Function:1
示例3
<?php
arr1 =arr2 = array("PHP12", "PHP10", "PHP2", "PHP1");
echo "Standard string comparisonn";
usort(arr1, "strcmp");
print_r(arr1);
echo ?<br>?;
echo "nNatural order string comparisonn";
usort(arr2, "strnatcmp");
print_r(arr2);
?>
輸出:
Standard string comparisonnArray
(
[0] => PHP1
[1] => PHP10
[2] => PHP12
[3] => PHP2
)
nNatural order string comparisonnArray
(
[0] => PHP1
[1] => PHP2
[2] => PHP10
[3] => PHP12
)
參見
- str_pad() :用于將字符串填充到新長度。愛掏網 - it200.com
- str_ireplace() :用于替換字符串中的一些字符(不區分大小寫)。愛掏網 - it200.com
- str_repeat() :用于重復一個字符串指定的次數。愛掏網 - it200.com
參考:
http://php.net/manual/en/function.strncmp.php
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。