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

search for in the

strncmp> <strnatcmp
[edit] Last updated: Fri, 17 May 2013

view this page in

strncasecmp

(PHP 4 >= 4.0.2, PHP 5)

strncasecmp二进制安全比较字符串开头的若干个字符(不区分大小写)

说明

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

该函数与 strcasecmp() 类似,不同之处在于你可以指定两个字符串比较时使用的长度(即最大比较长度)。

参数

str1

第一个字符串。

str2

第二个字符串。

len

最大比较长度。

返回值

如果 str1 小于 str2,返回负数;如果 str1 大于 str2,返回正数;二者相等则返回 0。

参见

  • preg_match() - 执行一个正则表达式匹配
  • strcmp() - 二进制安全字符串比较
  • strcasecmp() - 二进制安全比较字符串(不区分大小写)
  • substr() - 返回字符串的子串
  • stristr() - strstr 函数的忽略大小写版本
  • strstr() - 查找字符串的首次出现



add a note add a note User Contributed Notes strncasecmp - [2 notes]
up
0
khootz
6 years ago
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
up
0
djdykes
7 years ago
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

 
show source | credits | stats | sitemap | contact | advertising | mirror sites