note 31600 added to function.strcmp

From: Date: Sun, 27 Apr 2003 20:40:15 +0000
Subject: note 31600 added to function.strcmp
Groups: php.notes 
Request: Send a blank email to php-notes+get-47178@lists.php.net to get a copy of this message
Because of the way this function works, it's not very useful for ordering strings alphabetically. If you're trying to alphabetize a list of strings (like a dictionary does), you may want to use the following functions instead: ----------------- // Returns 1 if $str1 comes before $str2 alphabetically // Returns -1 if $str1 comes after $str2 alphabetically // Returns 0 if $str1 and $str2 are the same function orderAlpha ( $str1, $str2 ) { $limit = null; if ( strlen( $str1 ) > strlen( $str2 ) ) { $limit = strlen( $str2 ); } else { $limit = strlen( $str1 ); } for ( $i = 0;$i < $limit;$i++ ) { if ( substr( $str1, $i, 1 ) > substr( $str2, $i, 1 ) ) { return 1; } else if ( substr( $str1, $i, 1 ) < substr( $str2, $i, 1 ) ) { return -1; } } if ( strlen( $str1 ) > strlen( $str2 ) ) { return 1; } else if ( strlen( $str1 ) < strlen( $str2 ) ) { return -1; } return 0; } // Case insensitive version of orderAlpha function orderiAlpha ( $str1, $str2 ) { return orderAlpha( strtolower( $str1 ), strtolower( $str2 ) ); } ----------------- -- http://www.php.net/manual/en/function.strcmp.php http://master.php.net/manage/user-notes.php?action=edit+31600 http://master.php.net/manage/user-notes.php?action=delete+31600 http://master.php.net/manage/user-notes.php?action=reject+31600

« previous php.notes (#47178) next »