downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

strncmp> <strnatcmp
Last updated: Fri, 06 Nov 2009

view this page in

strncasecmp

(PHP 4 >= 4.0.2, PHP 5)

strncasecmpバイナリセーフで大文字小文字を区別しない文字列比較を、最初の n 文字について行う

説明

int strncasecmp ( string $str1 , string $str2 , int $len )

この関数は、strcasecmp() に似ていますが、 各文字列から比較する文字数(の上限)(len ) を指定できるという違いがあります。どちらかの文字列が len より短い場合、その文字列の長さが比較時に使用されます。

パラメータ

str1

最初の文字列。

str2

次の文字列。

len

比較する文字列の長さ。

返り値

str1str2 より短い場合に < 0 を返し、str1str2 より大きい場合に > 0、等しい場合に 0 を返します。

参考

  • preg_match() - 正規表現によるマッチングを行う
  • strcmp() - バイナリセーフな文字列比較
  • strcasecmp() - 大文字小文字を区別しないバイナリセーフな文字列比較を行う
  • substr() - 文字列の一部分を返す
  • stristr() - 大文字小文字を区別しない strstr
  • strstr() - 文字列が最初に現れる位置を見つける



add a note add a note User Contributed Notes
strncasecmp
khootz
28-Nov-2006 04:23
i assume you can also use the ! (not) operator

<?php

if (!strncasecmp($string, 'Trudeau', 4)){
       print
"true";
}

?>

since booleans in PHP translate 0 to FALSE and any other integer to TRUE.

!0  = !false = true
!1  = !true = false
!-1 = !true = false
djdykes
31-Aug-2005 10:43
Hi all,

be aware when comparing strings using the strcmp family. if you write code like this...

if (strncasecmp($string, 'Trudeau', 4))
 print "true";

The above code returns 1 which evaluates to boolean 'true'

that statement will always be true... because these functions return 0 only when equal. so a better test would be

if ( (strncasecmp($string, 'Trudeau', 4)) == 0)
 print "true";

always test these functions equality with 0

regards

strncmp> <strnatcmp
Last updated: Fri, 06 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites