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

search for in the

strpbrk> <strncasecmp
Last updated: Fri, 13 Nov 2009

view this page in

strncmp

(PHP 4, PHP 5)

strncmp最初の n 文字についてバイナリセーフな文字列比較を行う

説明

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

この関数は strcmp() に似ていますが、 各文字列から(最大)文字数(len ) を比較に使用するところが異なります。

比較は大文字小文字を区別することに注意してください。

パラメータ

str1

最初の文字列。

str2

次の文字列。

len

比較する文字数。

返り値

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

参考

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



add a note add a note User Contributed Notes
strncmp
codeguru at crazyprogrammer dot cba dot pl
25-Jan-2008 04:07
I ran the following experiment to compare arrays.

1 st - using (substr($key,0,5 == "HTTP_") & 2 nd - using (!strncmp($key, 'HTTP_', 5))

I wanted to work out the fastest way to get the first few characters from a array

BENCHMARK ITERATION RESULT IS:
if (substr($key,0,5 == "HTTP_").... -   0,000481s
if (!strncmp($key, 'HTTP_', 5)).... -     0,000405s

strncmp() is 20% faster than substr() :D

<?php
// SAMPLE FUNCTION
function strncmp_match($arr)
{
foreach (
$arr as $key => $val)
    {
   
//if (substr($key,0,5 == "HTTP_")
   
if (!strncmp($key, 'HTTP_', 5))   
        {
   
$out[$key] = $val;
        }
    }
return
$out;
}

// EXAMPLE USE
?><pre><?php
print_r
(strncmp_match($_SERVER));
?></pre>

will display code like this:

Array
(
    [HTTP_ACCEPT] => XXX
    [HTTP_ACCEPT_LANGUAGE] => pl
    [HTTP_UA_CPU] => x64
    [HTTP_ACCEPT_ENCODING] => gzip, deflate
    [HTTP_USER_AGENT] => Mozilla/4.0
                                    (compatible; MSIE 7.0;
                                     Windows NT 5.1;
                                    .NET CLR 1.1.4322;
                                    .NET CLR 2.0.50727)
    [HTTP_HOST] => XXX.XXX.XXX.XXX
    [HTTP_CONNECTION] => Keep-Alive
    [HTTP_COOKIE] => __utma=XX;__utmz=XX.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)
)
Anonymous
17-Apr-2002 08: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".

strpbrk> <strncasecmp
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites