當前位置: 首頁> 函數類別大全> 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 - 不區分大小寫
同類函數
  • 將字符串拆分為較小的塊 chunk_split

    chunk_split

    將字符串拆分為較小的塊
  • 使用uuencode 編碼一個字符串 convert_uuencode

    convert_uuencode

    使用uuencode編碼一個字符串
  • 將特殊的HTML 實體轉換回普通字符 htmlspecialchars_decode

    htmlspecialchars_decode

    將特殊的HTML實體轉換回普通字符
  • 使用反斜線轉義字符串 addslashes

    addslashes

    使用反斜線轉義字符串
  • 標記分割字符串 strtok

    strtok

    標記分割字符串
  • 設置區域設置信息 setlocale

    setlocale

    設置區域設置信息
  • 以千位分隔符方式格式化一個數字 number_format

    number_format

    以千位分隔符方式格式化一個數字
  • 從字符串的開頭和結尾去除空格(或其他字符) trim

    trim

    從字符串的開頭和結尾去除空格(或其他字符
熱門文章