String comparison and lookup for PHP learning

  • 2020-03-31 21:37:16
  • OfStack

1. String comparison
In PHP, you can compare strings with == (double equal sign) or === (triple equal sign). The difference between the two is that the double equal sign does not compare the type, the three equal sign will compare the type, it does not convert the type; When you compare with a double equal sign, if you have a numeric value on either side of the equality, you just convert the other value to a number and then compare. In this case, if it is a pure string or NULL, it will be converted to 0 for comparison. Again, the magnitude of the sign is the same as the equal sign, the comparison may appear incorrect results.
So you can compare strings using PHP's native STRCMP and strcasecmp functions. Strcasecmp is a variant of STRCMP that converts strings to lowercase and then compares them. The following code:
 
var_dump(0 == 'Test'); 
var_dump(0 == ''); 
var_dump(5 > 'T'); 
var_dump(strcmp(5, 'T')); 

The results are (the first 1-3 results are not correct, only the fourth is correct) :
 
bool(true) 
bool(true) 
bool(true) 
int(-1) 

2. String processing
1. The substring
$sub = substr(string, start[, length]);
2. Substring substitution
$newstring = substr_replace(string, new, start[, length]);
This function can be used to implement the string insert, delete operations. The start and length of this function can be negative. Respectively means that the calculation starts after and the last few bits are not replaced. 3. Reverse order of strings
$newstring = strrev (string);
Repeat the string
$newstring = str_repeat (string, the count);
Returns a new string that repeats count times the string.
5. Populate the string
$newstring = str_pad(to_pad, length[, with[, type]]);
STR_PAD_RIGHT (default), STR_PAD_LEFT, and STR_PAD_BOTH. With defaults to a space. The to_pad function populates the to_pad string with a string of length. The following code:
 
//substring
var_dump(substr('1234567890', 8)); // 90 
var_dump(substr('1234567890', 0, 2)); // 12 
//Inverse substring
var_dump(substr('1234567890', -8)); // 34567890 
var_dump(substr('1234567890', -8, -2)); // 345678 
var_dump(substr('1234567890', -8, 2)); // 34 
//insert
var_dump(substr_replace('1234567890', 'a', 0, 0)); // a1234567890 
//delete
var_dump(substr_replace('1234567890', '', 8)); // 12345678 
//Reverse deletion
var_dump(substr_replace('1234567890', '', -2, -1)); // 123456780 
//replace
var_dump(substr_replace('1234567890', 'a', 0, 1)); // a234567890 
//Reverse substitution
var_dump(substr_replace('1234567890', 'a', -2, -1)); // 12345678a0 
//String inversion
var_dump(strrev('1234567890')); // 0987654321 
//Duplicate string
var_dump(str_repeat('12', 3)); // 121212 
//Fill string
var_dump(str_pad('a', 10, '12')); // a121212121 
var_dump(str_pad('a', 10, '12', STR_PAD_LEFT)); // 121212121a 
var_dump(str_pad('a', 10, '12', STR_PAD_BOTH)); // 1212a12121 

3. Decompose the string
In PHP, strings are broken up with implode, combined with implode (join is an alias for implode), and marked with strtok. There is another function slipt that can also be decomposed (regular decomposition), but is not recommended after 5.3. There is also a sscanf() function in PHP that reads strings.
Strtok token, with strtok($STR, $token) to initialize, strtok($token) to continue to value.
The code is as follows:
 
$str = '1,2,3'; 
$arr1 = explode(',', $str); // array('1', '2', '3') 
$arr2 = explode(',', $str, 2); // array('1', '2,3') 
$str1 = implode(',', $arr1); // '1,2,3' 
$str2 = strtok($str, ','); // 1 
$str3 = strtok(','); // 2 
$str4 = strtok(','); // 3 
// array(86, 10, 88888888, 'Beijin') 
$arr3 = sscanf('+86(10)88888888 Beijin', '+%d(%d)%d %s'); 

4. String lookup
In PHP, there are three series of string lookups. Returns a match of position, string, and number of masks. There are two functions that return the position, strpos() and strrpos(). There are also two STRSTR () and STRCHR () that return a string; Functions that return the number of mask matches are STRSPN () and STRCSPN ().
Strpos means counting from the left, returning the first occurrence of the string you are looking for. Strrpos represents counting from the right, returning the first occurrence of the string to be looked for.
STRSTR represents counting from the left and returns the substring (including the search string) from the first time to the end of the string to be found. Stristr means no size lookup; STRCHR is an alias for STRSTR; STRRCHR returns the last substring of the character to appear at the end.
STRSPN represents the number of characters in the substring that count from the left before the first non-mask appears; STRCSPN represents the number of characters in the substring before the mask appears for the first time, counting from the left.
Sample code:
 
$pos = strpos('This a hello world program', ' '); // 4 
$pos = strpos('This a hello world program', 32); // 4 
$pos = strrpos('This a hello world program', ' '); // 18 
$pos = strrpos('This a hello world program', 32); // 18 
$str = strstr('This a hello world program', ' '); // " a hello world program" 
$str = strstr('This a hello world program', 32); // " a hello world program" 
$str = stristr('This a hello world program', ' A'); // "a hello world program" 
$str = stristr('This a hello world program', 65); // "a hello world program" 
$str = strrchr('This a hello world program', ' '); // " program" 
$str = strrchr('This a hello world program', 32); // " program" 
$str1 = "12345 12345 12345"; 
$len = strspn($str1, '12345'); // 5 
$len = strcspn($str1, ' '); // 5 

Resources: PHP programming, 2003, chapter 4 strings, string comparison; String lookup and processing

Related articles: