PHP Learning string comparison and find _php tutorials

Source: Internet
Author: User
Tags php programming strcmp strtok
1. String comparisons
In PHP, you can compare strings with = = (double equals) or = = = (three equals). The difference between the two is that the double equals is not a comparison type, the three equals is the comparison type, it does not convert the type, and if there is a number type value on both sides of the equal sign, the second value is converted to a number and then compared. 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, which may result in incorrect results when compared.
Therefore, the comparison string can be used with PHP's own function strcmp and strcasecmp. Where strcasecmp is a variant of strcmp, it converts the string to lowercase before comparing it. The following code:
Copy CodeThe code is 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 wrong, only the 4th one is right):
Copy CodeThe code is as follows:
BOOL (TRUE)
BOOL (TRUE)
BOOL (TRUE)
Int (-1)

2. String processing
1. Sub-string
$sub = substr (String, start[, length]);
2. Substring substitution
$newstring = Substr_replace (String, new, start[, length]);
Use this function to implement the insertion and deletion of strings. The start and length of this function can be negative. Each one is calculated from the back and retains the last few replacements. 3. String reverse Order
$newstring = Strrev (string);
4. Repeating strings
$newstring = Str_repeat (string, count);
Returns a new string that repeats the count of times string.
5. Populating strings
$newstring = Str_pad (To_pad, length[, with[, type]]);
Where type is: Str_pad_right (default), Str_pad_left, and Str_pad_both three; with default is a space. The function indicates that the To_pad string is populated with a string of length. The following code:
Copy CodeThe code is as follows:
Sub-string
Var_dump (substr (' 1234567890 ', 8)); 90
Var_dump (substr (' 1234567890 ', 0, 2)); 12
Inverse direction 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 Delete
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
padding string
Var_dump (Str_pad (' A ', 10, ' 12 ')); a121212121
Var_dump (Str_pad (' A ', ten, ' n ', str_pad_left)); 121212121a
Var_dump (Str_pad (' A ', ten, ' n ', Str_pad_both)); 1212a12121

3. Breaking Strings
In PHP, the decomposition of the string with explode, combined with implode (join is the alias of implode), tagged with strtok. There is another function slipt can also be decomposed (regular decomposition), but after 5.3 version is not recommended. In addition, PHP also has a sscanf () function, which is used to read the string.
When Strtok is marked, it is initialized with Strtok ($STR, $token) and continues to be evaluated with Strtok ($token).
The code is as follows:
Copy CodeThe code is as follows:
$str = ' All-in-all ';
$arr 1 = Explode (', ', $str); Array (' 1 ', ' 2 ', ' 3 ')
$arr 2 = Explode (', ', $str, 2); Array (' 1 ', ' 2,3 ')
$str 1 = implode (', ', $arr 1); ' A '
$str 2 = strtok ($str, ', '); 1
$str 3 = strtok (', '); 2
$str 4 = strtok (', '); 3
Array (88888888, ' Beijin ')
$arr 3 = sscanf (' +86 (Ten) 88888888 Beijin ', ' +%d (%d)%d%s ');

4. String Lookup
In PHP, the search for strings has three series. Returns the position, return string, and mask number matching. Where the function of the return position has two, strpos () and Strrpos (); The return string also has two strstr () and STRCHR (); The function that returns the number of mask matches is strspn () and strcspn ().
The strpos represents the first occurrence of the string to be searched, counting from the left, and the strrpos representing the first occurrence of the string to be searched.
Strstr represents the count from the left, returns the substring to find the first to the end of the string (including the lookup string), which can be represented by ASCII numbers when looking for a character, stristr for a size-insensitive lookup; STRCHR is an alias for Strstr. STRRCHR returns the substring at the end of the string.
The STRSPN represents the number of characters from the left count, the substring preceding the first non-mask, and the number of characters of the substring before the first occurrence of the mask before the STRCSPN represents the count from the left.
Example code:
Copy CodeThe code is 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

Reference: PHP Programming, 2003, fourth, string comparison, string lookup and processing

http://www.bkjia.com/PHPjc/323152.html www.bkjia.com true http://www.bkjia.com/PHPjc/323152.html techarticle 1. String comparison in PHP, you can compare strings with = = (double equals) or = = = (three equals). The difference between the two is that the double equals does not compare the type, the three equals will compare the type, it does not turn ...

  • 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.