Directly compare whether the strings are exactly the same,You can use "==" to do this, but sometimes more complex string comparisons may be required,Such as partial matching.
1.strcmp () function:This function compares strings.When comparing,Case sensitive.
Statement:strcmp (string str1, string str2)
This function compares the two string parameters passed in,If the two strings are exactly the same,Returns 0;if str1 follows str2 in lexicographic order, returns a positive number;If str1 is less than str2, a negative number is returned.
<?php $a="i want to fly"; $b="i want to climb"; $back=strcmp ($a, $b); if ($back>0) echo "$a is greater than $b"; elseif ($back<0) echo "$a is less than $b"; else echo "$a equals $b"; ?>
$a is greater than $b
2.strcasecmp ():This function is basically the same as the strcmp function.But when the function is compared,not case sensitive.
<?php $val1="hello"; $val2="hello"; if (strcasecmp ($val1, $val2) == 0) echo "$val1 and $val2 are the same (ignore the case of the string)"; ?>
$val1 and $val2 are the same (ignore the case of the string)
3. Natural order strnatcmp ():This function is basically the same as the strcmp function,But the principle of comparison is all different. The function is not in lexicographic order,Instead, strings are compared in "natural order".The so-called natural sort is to sort according to people's habits.E.g. strcmp function to sort,"4" would be greater than "14", and in reality,The number "14" is greater than "4", so the strnatcmp function is compared according to the latter.
4.strnatcasecmp ():This function is consistent with the use of strcasecmp function,Just the function is not case sensitive
- Examples of common PHP string processing functions [transform, escape, intercept, compare, find, reverse, cut]
- Summary of PHP's commonly used String function examples [conversion, replacement, calculation, interception, encryption]
- Usage and difference of replace string functions strtr () and str_repalce () in php
- PHP use strstr () function to get all characters after the specified string
- Summary of PHP common character function examples
- python 3x - typeerror: 'method' object is not subscriptable
- python - you may need to restart the kernel to use updated packages error
- xcode - pod install [!] no `podfile 'found in the project directory
- android studio - unresolved reference comes out in kotlin
- android studio - emulator: dsound: could not initialize about the error message directsoundcapture
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- django - oserror: [winerror 123] the file name, directory name, or volume label syntax is incorrect : '<frozen importlib_boot
- mysql startup failed [error] innodb: the innodb_system data file 'ibdata1' must be writable
- python - importerror: cannot import name md5 error cannot be resolved