strnatcasecmp

(PHP 4, PHP 5, PHP 7)

strnatcasecmp"자연순" 알고리즘을 이용한 대소문자 구분 없는 문자열 비교

설명

int strnatcasecmp ( string $str1 , string $str2 )

사람이 하는 것과 동일한 방법으로 알파벳/숫자 문자열의 순서를 비교하는 알고리즘을 수행합니다. 이 작동은 대소문자를 구분하지 않는 점을 제외하면, strnatcmp()와 동일합니다. 자세한 정보는 Martin Pool의 » 자연순 문자열 비교 페이지를 참고하십시오.

인수

str1

첫번째 문자열.

str2

두번째 문자열.

반환값

다른 문자열 비교 함수와 마찬가지로, str1str2보다 작으면 < 0을, str1str2보다 크면 > 0을, 같으면 0을 반환합니다.

참고

  • preg_match() - 정규표현식 매치를 수행
  • strcmp() - 바이너리 안전 문자열 비교
  • strcasecmp() - 대소문자 구분 없는 바이너리 안전 문자열 비교
  • substr() - Return part of a string
  • stristr() - 대소문자를 구분하지 않는 strstr
  • strncasecmp() - 대소문자 구분 없는 처음 n 문자의 바이너리 안전 문자열 비교
  • strncmp() - 처음 n 문자의 바이너리 안전 문자열 비교
  • strstr() - 문자열이 처음으로 나오는 부분을 찾습니다
  • setlocale() - Set locale information

add a note add a note

User Contributed Notes 3 notes

up
8
chatfielddaniel at googlemail dot com
13 years ago
The function treats '_' as after letters and numbers when it would be placed before logically.
up
2
Marco
7 years ago
Use strnatcmp to avoid the _ problem as mentioned below;

<<  The function treats '_' as after letters and numbers when it would be placed before logically. >>
up
-10
thomas at uninet dot se
17 years ago
There seems to be a bug in the localization for strnatcmp and strnatcasecmp. I searched the reported bugs and found a few entries which were up to four years old (but the problem still exists when using swedish characters).

These functions might work instead.
<?php
function _strnatcasecmp($left, $right) {
  return
_strnatcmp(strtolower($left), strtolower($right));
}

function
_strnatcmp($left, $right) {
  while((
strlen($left) > 0) && (strlen($right) > 0)) {
    if(
preg_match('/^([^0-9]*)([0-9].*)$/Us', $left, $lMatch)) {
     
$lTest = $lMatch[1];
     
$left = $lMatch[2];
    } else {
     
$lTest = $left;
     
$left = '';
    }
    if(
preg_match('/^([^0-9]*)([0-9].*)$/Us', $right, $rMatch)) {
     
$rTest = $rMatch[1];
     
$right = $rMatch[2];
    } else {
     
$rTest = $right;
     
$right = '';
    }
   
$test = strcmp($lTest, $rTest);
    if(
$test != 0) {
      return
$test;
    }
    if(
preg_match('/^([0-9]+)([^0-9].*)?$/Us', $left, $lMatch)) {
     
$lTest = intval($lMatch[1]);
     
$left = $lMatch[2];
    } else {
     
$lTest = 0;
    }
    if(
preg_match('/^([0-9]+)([^0-9].*)?$/Us', $right, $rMatch)) {
     
$rTest = intval($rMatch[1]);
     
$right = $rMatch[2];
    } else {
     
$rTest = 0;
    }
   
$test = $lTest - $rTest;
    if(
$test != 0) {
      return
$test;
    }
  }
  return
strcmp($left, $right);
}
?>

The code is not optimized. It was just made to solve my problem.
To Top