strncmp

(PHP 4, PHP 5, PHP 7, PHP 8)

strncmpString-Vergleich der ersten n Zeichen (binärsicher)

Beschreibung

strncmp(string $string1, string $string2, int $length): int

Diese Funktion ist ähnlich strcmp() mit dem Unterschied, dass die (maximale) Anzahl Zeichen angegeben werden kann, die bei jedem String verglichen werden sollen.

Es ist zu beachten, dass der Vergleich Groß- und Kleinschreibung berücksichtigt.

Parameter-Liste

string1

Die erste Zeichenkette.

string2

Die zweite Zeichenkette.

length

Die Anzahl der Zeichen, die für den Vergleich herangezogen werden soll.

Rückgabewerte

Gibt -1 zurück, wenn string1 in der Reihenfolge der Sortierung vor string2 kommt, 1, wenn string1 nach string2 kommt, und 0, wenn sie gleich sind.

Changelog

Version Beschreibung
8.2.0 Diese Funktion gibt nun -1 oder 1 zurück, wo sie vorher eine negative oder positive Zahl zurückgab.

Beispiele

Beispiel #1 strncmp()-Beispiel

<?php

$var1
= 'Hello John';
$var2 = 'Hello Doe';
if (
strncmp($var1, $var2, 5) === 0) {
echo
'Die ersten 5 Zeichen von $var1 und $var2 sind bei einem Vergleich von Strings unter Berücksichtigung der Groß- und Kleinschreibung gleich';
}
?>

Siehe auch

  • strncasecmp() - Binärsicherer Vergleich von Zeichenketten der ersten n Zeichen ohne Berücksichtigung der Groß- und Kleinschreibung
  • preg_match() - Führt eine Suche mit einem regulären Ausdruck durch
  • substr_compare() - Binärsicherer Vergleich zweier Strings, beginnend an einer bestimmten Position und endend nach einer festgelegten Länge
  • strcmp() - Vergleich zweier Strings (binärsicher)
  • strstr() - Findet das erste Vorkommen eines Strings
  • substr() - Liefert einen Teil eines Strings

add a note add a note

User Contributed Notes 5 notes

up
8
salehrezq at gmail dot com
7 years ago
A note not included in the documentation:

int strcmp ( string $str1 , string $str2 )

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

My addendum:
If str1 and str2 are not equal, and str1 is a sub-string of str2 or vise versa. The returned int value will be negative or positive indicating how many characters the difference is between the two strings in absolute terms.

Example:

<?php
$str1
= "phpaaa";
$str2 = "php";

echo
strcmp($str1, $str2); // 3
?>

since str2 = "php" is a sub-string of str1 = "phpaaa" and "phpaaa" is greater than "php" the returned value is positive and is 3 indicating how many characters the difference is between the two strings.

If you replace the value of str1 with str2 the result will be -3 (negative) but still indicates the absolute difference which is 3
up
7
codeguru at crazyprogrammer dot cba dot pl
16 years ago
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)
)
up
3
samy
6 years ago
I just want to highlight that (at least on php7), when testing for the existence of a string in the beginning of another string you should consider using substr  or strpos (if performances is an issue).

Here is a small benchmark (for what it's worth):
<?php
$n
= 'abcd';
$l = strlen($n);
$haystack0 = base64_encode(random_bytes(128));

//heat
$r = 1;
for (
$i = 0; $i < 100000000; $i++)
   
$r += $r * $r % 10000;

//tests
$k = 30000000;
$res = array();
foreach (array(
'found' => $n . $haystack0, 'not-found' => strrev($n) . $haystack0) as $f => $haystack) {
   
$m = microtime(true);
    for (
$i = 0; $i < $k; $i++)
        !
strncmp($haystack, $n, $l) && $r++;
   
$res["strncmp-$f"] = -$m + ($m = microtime(true));

    for (
$i = 0; $i < $k; $i++)
        (
strpos($haystack, $n) === 0) && $r++;
   
$res["strpos-$f"] = -$m + ($m = microtime(true));

    for (
$i = 0; $i < $k; $i++)
        (
substr($haystack, 0, $l) === $n) && $r++;
   
$res["substr-$f"] = microtime(true) - $m;
}

//print
asort($res);
print_r($res);
echo
"\n$r"; // makes sure no auto-optimization occurs
?>

This outputs:
<?php /*
    [strpos-found]        => 1.3313138484955
    [substr-not-found]    => 1.4832630157471
    [substr-found]        => 1.6976611614227
    [strpos-not-found]    => 2.0043320655823
    [strncmp-not-found]    => 2.0969619750977
    [strncmp-found]        => 2.3616981506348
*/
?>
up
2
bobvin at pillars dot net
12 years ago
For checking matches at the beginning of a short string, strpos() is about 15% faster than strncmp().

Here's a benchmark program to prove it:

<?php
$haystack
= "abcdefghijklmnopqrstuvwxyz";
$needles = array('abc', 'xyz', '123');
foreach (
$needles as $needle) {
 
$times['strncmp'][$needle] = -microtime(true);
  for (
$i = 0; $i < 1000000; $i++) {
   
$result = strncmp($haystack, $needle, 3) === 0;
  }
 
$times['strncmp'][$needle] += microtime(true);
}
foreach (
$needles as $needle) {
 
$times['strpos'][$needle] = -microtime(true);
  for (
$i = 0; $i < 1000000; $i++) {
   
$result = strpos($haystack, $needle) === 0;
  }
 
$times['strpos'][$needle] += microtime(true);
}
var_export($times);
?>
up
2
elloromtz at gmail dot com
13 years ago
if length is 0 regardless what the two strings are, it will return 0

<?php
strncmp
("xybc","a3234",0); // 0
strncmp("blah123","hohoho", 0); //0
?>
To Top