strncmp

(PHP 4, PHP 5)

strncmp --  Сравнение первых n символов строк без учета регистра, безопасное для данных в двоичной форме

Описание

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

Эта функция подобна strcmp(), за исключением того, что можно указать максимальное количество символов в обоих строках, которые будут участвовать в сравнении.

Возвращает отрицательное число, если str1 меньше, чем str2; положительное число, если str1 больше, чем str2, и 0 если строки равны.

Эта функция учитывает регистр символов.

См. также описание функций ereg(), strncasecmp(), strcasecmp(), substr(), stristr(), strcmp() и strstr().



strncmp
Anonymous
17-Apr-2002 04: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: Tue, 15 Nov 2005