PHP Learning string comparisons and find _php tips

Source: Internet
Author: User
Tags explode numeric value php programming strcmp strtok
1. String comparison
In PHP, you can compare strings with either = = (double equals) or = = = (three equals). The difference is that the double equals does not compare the type, the three equals are the type, it does not convert the type; When you compare a double equal sign, if you have a numeric value on both sides of the equal sign, you just convert the other value to a number and then compare it. In this case, if it is a pure string or null, it is converted to 0 for comparison. Similarly, the size of the number is the same as the equal sign, the comparison may appear incorrect results.
So, comparing strings can be strcmp and strcasecmp with PHP's own functions. Where strcasecmp is a variant of the strcmp, it converts the string to lowercase before comparing it. The following code:
Copy Code code as follows:

Var_dump (0 = ' Test ');
Var_dump (0 = = ");
Var_dump (5 > ' T ');
Var_dump (strcmp (5, ' T '));

The result is (the 1th to 3rd result is incorrect, only the 4th is correct):
Copy Code code as follows:

BOOL (TRUE)
BOOL (TRUE)
BOOL (TRUE)
Int (-1)

2. String processing
1. Sub-string
$sub = substr (String, start[, length]);
2. Sub-string substitution
$newstring = Substr_replace (String, new, start[, length]);
Use this function to implement the insertion of strings, delete operations. The start and length of this function can be negative. Represents the calculation from the beginning and the last few substitutions. 3. String reverse Sequence
$newstring = Strrev (string);
4. Duplicate string
$newstring = Str_repeat (string, count);
Returns a new string that repeats the count times string.
5. Fill string
$newstring = Str_pad (To_pad, length[, with[, type]);
The types are: Str_pad_right (default), Str_pad_left, and Str_pad_both three; with the default is a space. The function indicates that the To_pad string is filled with a string of length. The following code:
Copy Code code as follows:

Sub string
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 direction deletion
Var_dump (Substr_replace (' 1234567890 ', ',-2,-1)); 123456780
Replace
Var_dump (Substr_replace (' 1234567890 ', ' A ', 0, 1)); a234567890
Reverse direction substitution
Var_dump (Substr_replace (' 1234567890 ', ' a ',-2,-1)); 12345678a0
String inversion
Var_dump (Strrev (' 1234567890 ')); 0987654321
Repeating string
Var_dump (Str_repeat (' 12 ', 3)); 121212
Fill string
Var_dump (Str_pad (' A ', 10, ' 12 ')); a121212121
Var_dump (Str_pad (' A ', ten, ', ', str_pad_left)); 121212121a
Var_dump (Str_pad (' A ', ten, ', ', Str_pad_both)); 1212a12121

3. Decomposition string
In PHP, the decomposition of strings is explode, combined with implode (join is implode alias), labeled with Strtok. There is another function slipt can also be decomposed (regular decomposition), but after 5.3 version has not been recommended. There is also a sscanf () function in PHP to read the string.
When the strtok tag is initialized with Strtok ($str, $token), the value is continued with Strtok ($token).
The code is as follows:
Copy Code code as follows:

$str = ' 1,2,3 ';
$arr 1 = Explode (', ', $str); Array (' 1 ', ' 2 ', ' 3 ')
$arr 2 = Explode (', ', $str, 2); Array (' 1 ', ' 2,3 ')
$str 1 = implode (', ', $arr 1); ' 1,2,3 '
$str 2 = strtok ($str, ', '); 1
$str 3 = strtok (', '); 2
$str 4 = strtok (', '); 3
Array (88888888, ' Beijin ')
$arr 3 = sscanf (' +86 () 88888888 Beijin ', ' +%d (%d)%d%s ');

4. String Lookup
In PHP, the search for strings has three series. Matches the number of masks that return a position, and return a string. Where the function of the return position is two, Strpos () and Strrpos (), there are also two strstr () and STRCHR () of the returned string, and a function that returns the number of mask matches strspn () and strcspn ().
Strpos represents the start of the count from the left, returning the first occurrence of the string to find; Strrpos represents counting from the right, returning the first occurrence of the string to find.
Strstr represents the count from the left, returning the substring (including the lookup string) to find the first time to the end of the string, which can be used to represent the character when the character is found, and stristr to indicate a case-insensitive lookup; STRCHR is strstr alias STRRCHR returns the substring at which the character last appears to the end.
STRSPN represents the count from the left, the first occurrence of the number of substrings before the mask, and the number of characters in the substring preceding the mask, strcspn the count from the left.
Sample code:
Copy Code code as follows:

$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"
$str 1 = "12345 12345 12345";
$len = strspn ($str 1, ' 12345 '); 5
$len = strcspn ($str 1, "); 5

References: PHP programming, 2003, fourth chapter strings, string comparisons; string lookup and processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.