search for in the  
<strncasecmpstrpbrk>
Last updated: Thu, 19 May 2005

strncmp

(PHP 4, PHP 5)

strncmp --  Binary safe string comparison of the first n characters

Description

int strncmp ( string str1, string str2, int len )

This function is similar to strcmp(), with the difference that you can specify the (upper limit of the) number of characters (len) from each string to be used in the comparison.

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

Note that this comparison is case sensitive.

See also ereg(), strncasecmp(), strcasecmp(), substr(), stristr(), strcmp(), and strstr().



User Contributed Notes
strncmp
Jules
15-Jan-2003 07:41
To do this on a system where this function is not available you could use code like the following:

function my_strncmp($cmp1, $cmp2, $limit)
{
   if (strlen ($cmp1) > $limit)
       $cmp1 = substr($cmp1, 0, $limit);
   if (strlen ($cmp2) > $limit)
         $cmp2 = substr($cmp2, 0, $limit);
   return strcmp ($cmp1, $cmp2);
}
08-Jun-2002 07:33
To osvaldojaneri@uol.com.br,
You can use substr(string s, int start, [int length]) and then strcmp() the returned string, or make your own strncmp() that should be more efficient ;)
Anonymous
17-Apr-2002 06:46
strncmp("sample","sam",4) returns 1 because the final requirement is if one string terminates before len, then the other must also terminate at that position. 

You can imagine that all your strings have one more final, invisible "termination" character.  If that termination character happens to be within in len, then it must match, too.

For instance, write that termination character with, say, the sequence "\0". Then you can equivalently consider that function call as strncmp("sample\0","sam\0",4).

So, the "p" in "sample" does not match the termination character in "sam".

<strncasecmpstrpbrk>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT