當前位置: 首頁> 函數類別大全> substr_compare

substr_compare

二進制安全比較字符串(從偏移位置比較指定長度)
名稱:substr_compare
分類:字符串
所屬語言:php
一句話介紹:從指定的開始位置(二進制安全和選擇性區分大小寫)比較兩個字符串。

定義和用法

substr_compare()函數從指定的開始位置比較兩個字符串。

提示:該函數是二進制安全且選擇性地對大小寫敏感。

實例

例子1

比較兩個字符串:

 <?php
echo substr_compare ( "Hello world" , "Hello world" , 0 ) ;
?>

親自試一試

例子2

比較兩個字符串,當string1中供比較的開始位置為6 時:

 <?php
echo substr_compare ( "Hello world" , "world" , 6 ) ;
?>

親自試一試

例子3

使用所有的參數:

 <?php
echo substr_compare ( "world" , "or" , 1 , 2 ) ;
echo substr_compare ( "world" , "ld" , - 2 , 2 ) ;
echo substr_compare ( "world" , "orl" , 1 , 2 ) ;
echo substr_compare ( "world" , "OR" , 1 , 2 , TRUE ) ;
echo substr_compare ( "world" , "or" , 1 , 3 ) ;
echo substr_compare ( "world" , "rl" , 1 , 2 ) ;
?>

親自試一試

例子4

不同的返回值:

 <?php
echo substr_compare ( "Hello world!" , "Hello world!" , 0 ) ; // 兩字符串相等
echo substr_compare ( "Hello world!" , "Hello" , 0 ) ; // string1大於string2
echo substr_compare ( "Hello world!" , "Hello world! Hello!" , 0 ) ; // string1小於string2
?>

親自試一試

文法

substr_compare ( string1 , string2 , startpos , length , case )
參數描述
string1必需。規定要比較的第一個字符串。
string2必需。規定要比較的第二個字符串。
startpos必需。規定在string1中的何處開始比較。如果為負數,則從字符串末端開始計數。
length可選。規定對string1中的多少字符進行比較(字符數)。
case

可選。布爾值,規定是否執行區分大小寫的比較:

  • FALSE - 默認。區分大小寫
  • TRUE - 不區分大小寫
同類函數
  • 查找字符串中子字符串第一次出現的位置 strpos

    strpos

    查找字符串中子字符串第一次出現的位置
  • 計算文件的sha1哈希值 sha1_file

    sha1_file

    計算文件的sha1哈希值
  • 隨機打亂一個字符串 str_shuffle

    str_shuffle

    隨機打亂一個字符串
  • 二進制安全比較字符串(從偏移位置比較指定長度) substr_compare

    substr_compare

    二進制安全比較字符串(從偏移位置比較指定
  • 使用“自然順序”算法比較字符串(不區分大小寫) strnatcasecmp

    strnatcasecmp

    使用“自然順序”算法比較字符串(不區分大
  • 重複一個字符串 str_repeat

    str_repeat

    重複一個字符串
  • 將字符串拆分為較小的塊 chunk_split

    chunk_split

    將字符串拆分為較小的塊
  • 二進制安全字符串比較 strcmp

    strcmp

    二進制安全字符串比較
熱門文章