PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




strnatcmp

Name

strnatcmp — Compares strings using a "natural" algorithm.

Synopsis

int strnatcmp(string_one, string_two);
string string_one: First string to compare
string string_two: Second string to compare

Returns

Integer

Description

strnatcmp() compares strings in much the same fashion as a human would. Numbers are ordered by their value, instead of by their character value.

Version

PHP 4.0RC2+

See also

To perform non-case-sensitive natural string comparison:

strnatcasecmp()

For additional information on natural sorting:

Martin Pool's Natural Order String Comparison



Example

Example 1225. Compare a list of values

<?php
$strings = array("one", 1, "01", 2001, "two-thousand and one");

foreach($strings as $string_one) {
   foreach($strings as $string_two) {
      $comparison = strnatcmp($string_one, $string_two);

      if ($comparison < 0) {
         echo "$string_one is less than $string_two\n";
      } else if ($comparison == 0) {
         echo "$string_one is equal to $string_two\n";
      } else {
         echo "$string_one is greater than $string_two\n";
      }
   }
}

?>

Output:one is equal to one
one is greater than 1
one is greater than 01
one is greater than 2001
one is less than two-thousand and one
1 is less than one
1 is equal to 1
1 is greater than 01
1 is less than 2001
1 is less than two-thousand and one
01 is less than one
01 is less than 1
01 is equal to 01
01 is less than 2001
01 is less than two-thousand and one
2001 is less than one
2001 is greater than 1
2001 is greater than 01
2001 is equal to 2001
2001 is less than two-thousand and one
two-thousand and one is greater than one
two-thousand and one is greater than 1
two-thousand and one is greater than 01
two-thousand and one is greater than 2001
two-thousand and one is equal to two-thousand and one



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.