HOWTO · PHP
PHP는 문자열을 비교할 때 대소문자 구분을 무시합니다.
이 튜토리얼은 PHP에서 대소문자를 무시하는 방법을 보여줍니다.
이 페이지의 내용
이 튜토리얼은 PHP에서 대소문자를 무시하는 방법을 보여줍니다.
PHP는 문자열을 비교할 때 대소문자 구분을 무시합니다.
때때로 두 문자열을 비교할 때 두 문자열의 대소문자를 무시해야 합니다. PHP는 대소문자를 무시하면서 두 문자열을 비교하는 내장 메서드 strcasecmp()를 제공합니다.
strcasecmp() 메서드는 두 개의 매개변수를 사용합니다. 둘 다 비교할 문자열입니다. 이 메서드는 다음 값을 반환합니다.
strcasecmp()는 두 문자열이 같으면 0을 반환합니다.strcasecmp()는 첫 번째 문자열이 두 번째 문자열보다 작은 경우 <0을 반환합니다.strcasecmp()는 첫 번째 문자열이 두 번째 문자열보다 크면 >0을 반환합니다.
이 메서드는 문자열만 비교한 다음 값을 반환합니다. 문자열의 대소문자만 다를 경우 항상 0을 반환합니다.
예를 참조하십시오:
<?php
$String1 = "This is Delftstack.com";
$String2 = "This is Delftstack.com";
// Both the strings are equal in case
$Result=strcasecmp($String1, $String2);
echo "The result for two equal strings is: ".$Result."<br>";
$String1 = "this is delftstack.com";
$String2 = "THIS IS DELFTSTACK.COM";
// first string is lowercase than the second string
$Result=strcasecmp($String1, $String2);
echo "The result for first string lowercase and second string uppercase is : ".$Result."<br>";
$String1 = "THIS IS DELFTSTACK.COM";
$String2 = "this is delftstack.com";
// first string is uppercase, then the second string
$Result=strcasecmp($String1, $String2);
echo "The result for first string uppercse and second string lowercase is: ".$Result;
?>
위의 코드는 문자열이 서로 다른 경우를 무시하면서 문자열을 비교합니다. 출력 참조:
The result for two equal strings is: 0
The result for first string lowercase and second string uppercase is : 0
The result for first string uppercse and second string lowercase is: 0
strcasecmp() 메서드에서 볼 수 있듯이 소문자, 대문자 및 문장의 모든 문자열은 동일합니다. 이것이 항상 0을 반환하는 이유입니다. 문자열이 단어나 문자와 같지 않은 경우를 예로 들어 보겠습니다.:
<?php
$String1 = "delftstack.com";
$String2 = "THIS IS DELFTSTACK.COM";
// first string is lowercase, then the second string
$Result=strcasecmp($String1, $String2);
echo "The result for first string is lower then second string : ".$Result."<br>";
$String1 = "THIS IS DELFTSTACK.COM";
$String2 = "delftstack.com";
// First string is greater then second string
$Result=strcasecmp($String1, $String2);
echo "The result for first string is greater then second string: ".$Result;
?>
strcasecmp() 메서드는 음수 또는 양수로 숫자 값을 반환합니다. 출력 참조:
The result for first string is lower then second string : -16
The result for first string is greater then second string: 16