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
strncasecmp
(PHP 4 >= 4.0.2, PHP 5)
strncasecmp — Binary safe case-insensitive σύγκριση των πρώτων n χαρακτήρων δύο string
Περιγραφή
int strncasecmp
( string $str1
, string $str2
, int $len
)
Η συνάρτηση αυτή είναι όμοια με τη συνάρτηση strcasecmp(), με τη μόνη διαφορά ότι μπορείτε να ορίσετε το (πάνω όριο του) πλήθος των χαρακτήρων (len ) που θα χρησιμοποιηθούν από κάθε string για τη σύγκριση. Εάν κάποιο από τα strings είναι μικρότερο του len , τότε για τη σύγκριση θα χρησιμοποιηθεί το μήκος αυτού του string.
Επιστρέφει < 0 εάν η παράμετρος str1 είναι μικρότερη της str2 , > 0 εάν η str1 είναι μεγαλύτερη της str2 , και 0 εάν οι δύο παράμετροι είναι ίσες.
Ανατρέξτε επίσης στις: ereg(), strcasecmp(), strcmp(), substr(), stristr(), και strstr().
strncasecmp
khootz
27-Nov-2006 08:23
27-Nov-2006 08:23
djdykes
31-Aug-2005 03:43
31-Aug-2005 03:43
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
