Comparison and search of PHP learning strings

Source: Internet
Author: User
Tags strtok

1. String comparison
In PHP, you can use = (double equal sign) OR = (third equal sign) to compare strings. The difference between the two is that the two equal signs do not compare the type, and the third equal sign will compare the type, it does not convert the type; when the two equal signs are compared, if there is a number type value on both sides of the equal sign, I will convert another 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 greater than the equal sign is the same. incorrect results may occur during comparison.
Therefore, the strcmp and strcasecmp built-in functions of PHP can be used to compare strings. Strcasecmp is a variant of strcmp, which converts the string to lowercase before comparison. 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 (1st ~ 3. The result is incorrect. Only 4th are correct ):
Copy codeThe Code is as follows:
Bool (true)
Bool (true)
Bool (true)
Int (-1)

2. string processing
1. substring
$ Sub = substr (string, start [, length]);
2. substring replacement
$ Newstring = substr_replace (string, new, start [, length]);
This function can be used to insert and delete strings. The start and length values of this function can be negative. It indicates that the last few digits are not replaced. 3. Reverse string order
$ Newstring = strrev (string );
4. Duplicate strings
$ Newstring = str_repeat (string, count );
Returns a new string that repeats the count string.
5. Fill in the string
$ Newstring = str_pad (to_pad, length [, with [, type]);
There are three types: STR_PAD_RIGHT (default), STR_PAD_LEFT, and STR_PAD_BOTH. with is a space by default. The function is used to fill the to_pad string with a string of length. The following code:
Copy codeThe Code is as follows:
// Substring
Var_dump (substr ('123', 8); // 90
Var_dump (substr ('123', 0, 2); // 12
// Inverse substring
Var_dump (substr ('20140901',-8); // 1234567890
Var_dump (substr ('20140901',-8,-2); // 1234567890
Var_dump (substr ('20140901',-8, 2); // 34
// Insert
Var_dump (substr_replace ('20170101', 'A', 0, 0); // a1234567890
// Delete
Var_dump (substr_replace ('20140901', '', 8); // 1234567890
// Reverse Deletion
Var_dump (substr_replace ('20140901', '',-2,-1); // 1234567890
// Replace
Var_dump (substr_replace ('20170101', 'A', 0, 1); // a234567890
// Reverse replacement
Var_dump (substr_replace ('20140901', 'A',-2,-1); // 12345678a0
// String Inversion
Var_dump (strrev ('20140901'); // 1234567890
// Repeat the string
Var_dump (str_repeat ('12', 3); // 121212
// Fill the string
Var_dump (str_pad ('A', 10, '12'); // a1212121
Var_dump (str_pad ('A', 10, '12', STR_PAD_LEFT); // 121212121a
Var_dump (str_pad ('A', 10, '12', STR_PAD_BOTH); // 12a12121

3. Break down strings
In PHP, the string is decomposed using explode, combined with implode (join is the alias of implode), and marked with strtok. There is also another function slipt that can be decomposed (Regular Expression Decomposition), but it is not recommended in Versions later than 5.3. In addition, PHP also has an sscanf () function for reading strings.
When the strtok tag is used, use strtok ($ str, $ token) for initialization, and use strtok ($ token) to continue the value.
The Code is as follows:
Copy codeThe 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,888 88888, 'beijing ')
$ Arr3 = sscanf ('+ 86 (10) 88888888 Beijin', '+ % d (% d) % d % s ');

4. string SEARCH
In PHP, there are three series of string searches. Matches the return position, the returned string, and the number of masks. The return position function has two types: strpos () and strrpos (). The return string also has two strstr () and strchr (); the functions that return the number of mask matches include strspns () and strcspns ().
Strpos indicates counting from the left, returns the position where the string to be searched appears for the first time, strrpos indicates counting from the Right, and returns the position where the string to be searched appears for the first time.
Strstr indicates the count from the left, and returns the substring (including the string to be searched) from the first time to the end of the string. When the string is searched for, it can be represented by ascii numbers; stristr indicates that the query size is not distinguished; strchr is the alias of strstr; strrchr returns the substring from the last occurrence to the end of the character.
Strspns indicate the count from the left, the number of characters of the substring before the first occurrence of the non-mask; strcspns indicate the count from the left, and the number of characters of the substring before the first occurrence of the mask.
Sample 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"
$ Str1 = "12345 12345 12345 ";
$ Len = strspns ($ str1, '123'); // 5
$ Len = strcspns ($ str1, ''); // 5

Reference: PHP programming, chapter 4, character strings, string comparison, string SEARCH 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.