PHP substr_compare()関数
例
2つの文字列を比較します。
<?php
echo substr_compare("Hello world","Hello world",0);
?>
定義と使用法
substr_compare()関数は、指定された開始位置からの2つの文字列を比較します。
ヒント:この関数はバイナリセーフであり、オプションで大文字と小文字を区別します。
構文
substr_compare(string1,string2,startpos,length,case)
パラメータ値
Parameter | Description |
---|---|
string1 | Required. Specifies the first string to compare |
string2 | Required. Specifies the second string to compare |
startpos | Required. Specifies where to start comparing in string1. If negative, it starts counting from the end of the string |
length | Optional. Specifies how much of string1 to compare |
case | Optional. A boolean value that specifies whether or not to perform a case-sensitive compare:
|
技術的な詳細
戻り値: | この関数は次を返します。
|
---|---|
PHPバージョン: | 5歳以上 |
変更ログ: | PHP 5.5.11以降-長さパラメーターは0 にすることができます。PHP5.1以降、負のstartposを使用できるようになりました。 |
その他の例
例
比較のためのstring1の開始位置が6番目の場合、2つの文字列を比較します。
<?php
echo substr_compare("Hello world","world",6);
?>
例
すべてのパラメーターの使用:
<?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);
?>
例
さまざまな戻り値:
<?php
echo substr_compare("Hello world!","Hello world!",0); // the two strings are equal
echo substr_compare("Hello world!","Hello",0); // string1 is greater than string2
echo substr_compare("Hello world!","Hello world! Hello!",0); // str1 is less than str2
?>
❮PHP文字列リファレンス