PHP learning notes-string operations
1. remove spaces at the beginning and end of the string and special characters
The trim () function is used to remove the start and end characters.
Syntax string trim (string str [, string charlist]), where charlist is an optional parameter. it specifies the deleted string. if it is carried, it deletes the specified character. if it is not carried, delete all optional characters.
Optional characters include \ 0 (null), \ t (TAB), \ n (linefeed), \ x0B (vertical tab), \ r (carriage return), and "" (space)
The ltrim () function is used to remove spaces or specified characters on the left.
Syntax string ltrim (string str [, string charlist]), where charlist is an optional parameter, specifying the deleted string, if carried, delete the specified character, if not carried, delete all optional characters.
Optional characters include \ 0 (null), \ t (TAB), \ n (linefeed), \ x0B (vertical tab), \ r (carriage return), and "" (space)
The rtrim () function is used to remove spaces or specified characters on the right.
Syntax string ltrim (string str [, string charlist]), where charlist is an optional parameter, specifying the deleted string, if carried, delete the specified character, if not carried, delete all optional characters.
Optional characters include \ 0 (null), \ t (TAB), \ n (linefeed), \ x0B (vertical tab), \ r (carriage return), and "" (space)
2. escape character strings.
Escape character strings can be manually converted or automatically escaped.
Manually escape: use the escape character "\", for example:
Echo "this's a \" cat \ ";" // this's a "cat ";
Automatic escape: for simple strings, you can use manual escape. if the data size is compared, it is best to use automatic escape, mainly using the following function;
Addslashes () is used to add a slash "\" to the string "\";
Syntax string addslashes (string str );
Stripslashes () is used to restore the character string after addslashes () escape; for example:
$ Str = "select * from table_student where name = 'Jim '";
Echo $ str ."
"; // Select * from table_student where name = 'Jim'
Echo addslashes ($ str); // select * from table_student where name = \ 'Jim \'
Echo stripslashes (addslashes ($ str); // select * from table_student where name = 'Jim'
?>
Note: Before inserting data into the database, it is necessary to use the addslashes () function to escape the data to prevent special characters from being escaped. errors may occur when inserting data into the database.
Addcslashes () is used to add a backslash before a specified character;
Syntax string addcslashes (string str, string charlist)
Stripcslashes () is used to restore the string after the addcslashes () function is escaped;
Syntax string stripcslashes (string str)
$ Str = "select * from table_student where name = 'Jim '";
Echo $ str ."
"; // Select * from table_student where name = 'Jim'
Echo addcslashes ($ str, "'Jim'"); // select * fro \ m table_student where na \ me = \ '\ j \ I \ m \'
Echo stripcslashes (addcslashes ($ str, "'Jim '"); // select * from table_student where name = 'Jim'
?>
3. get the length of the string
Function strlen () to obtain the string length
$ Str = "select * from table_student where name = 'Jim '";
Echo strlen ($ str); // str string length: 46
?>
4. truncate a string
Function string substr (string str, int start [, int length])
Here, 'str' is the original string to be intercepted, and 'Start' is the starting position. Note that the first character of the string is 0. the optional parameter length is the intercepted length. if it is not carried, it refers to the truncation to the end. if it is a negative number, it refers to the truncation to the last few characters
$ Str = "select * from table_student where name = 'Jim '";
Echo substr ($ str, 2); // The optional parameter length is not included.
Echo substr ($ str, 3, 20); // capture from 3 to 20
Echo substr ($ str, 3,-1); // capture from 3 to the second to last
?>
5. compare strings
There are three methods: 1. byte 2. Sort by nature 3. specify the position of the source string for comparison
1). by byte
Use the following two functions: 1. strcmp () 2. strasecmp ()
A. strcmp (string str1, string str2) is case sensitive. if the value is equal, 0 is returned. if the value of str1 is greater than 0, 0 is returned. if the value of str2 is greater than 0, 0 is returned.
B. strcasecmp (string str1, string str2) is case insensitive
For example
$ Str1 = "abc ";
$ Str2 = "ABC ";
Echo strcmp ($ str1, $ str2 )."
"; // Case sensitive
Echo strcasecmp ($ str1, $ str2 )."
"; // Case insensitive
?>
2). Sort by nature
Sort by nature. when the characters in the string are the same, the numbers in the string are usually used to compare strings such as str1, str2, str3. ..
The int strnatcmp (string str1, string str2) function is case sensitive and returns 0 if the values are equal. if the value of str1 is greater than 0, returns less than 0 if the value of str2 is greater than 0.
$ Str1 = "abc2 ";
$ Str2 = "abc10 ";
Echo strcmp ($ str1, $ str2 )."
"; // Returns 1 and str1 in byte ratio, because 2 is greater than 1 in the computer
Echo strnatcmp ($ str1, $ str2 )."
"; // Sort by nature, return-1, str2, because by number ratio, 10 is greater than 2
?>
3). specify the position of the source string
The int strncmp (string str1, str2, int len) function is used to compare the first n characters in a string, as shown in
$ Str1 = "abce ";
$ Str2 = "abcdf ";
Echo strncmp ($ str1, $ str2, 4 )."
"; // Returns 1, comparing the first 4 characters of two strings
?>
6. string retrieval
1). function string strstr (string haystack, string needle)
Obtain the needle string that first appears at the end of haystack in haystack. if the string exists, the child string above is returned. if not, false is returned, note that the function must be case sensitive.
$ Str1 = "abce ";
$ Str2 = "abcdf ";
$ Str3 = "bcd ";
Echo strstr ($ str1, $ str2 )."
"; // Return false
Echo strstr ($ str2, $ str3); // return bcdf
?>
2) The int substr_count (string haystack, string needle) function queries the number of times needle appears on the haystack,
$ Str1 = "abc ";
$ Str2 = "abcdfabccdabc ";
$ Str3 = "eca ";
Echo substr_count ($ str2, $ str1 )."
"; // Return 3
Echo substr_count ($ str2, $ str3 )."
"; // Return 0
?>
7. replace string
1 ). str_ireplace (mix search, mix replace, mixed subject [, int & count]) searches for search in subject and replaces search with replace. & count indicates the number of times the string is executed, note that this function is case insensitive. if it is case sensitive, use str_replace ()
Echo str_ireplace ("WORlD", "John", "Hello world! World ", $ I); // Hello John! John
Echo $ I; // I is 2, which indicates that a total of 2 replicas are replaced.
?>
2 ). function substr_replace (string str, string repl, int start [, int length]) In string str, replace the string repl from the start position to the end, the optional parameter length indicates the length of the replacement. if it is null, it means that the length starts from the beginning to the end.
$ Str = "abcdefghijljljljljljljljlj ";
$ Repl = "12121231 ";
Echo substr_replace ($ str, $ repl, 2 )."
"; // The length parameter is null, always replaced with the end, ab12121231
Echo substr_replace ($ str, $ repl, 2, 3); // The length is 3, ab12121231fghijljljlljljljljljljljlj
?>
8. split the string
The array explode (string separator, string str [, int limit]) function uses the separator to split str. the limit parameter is an optional parameter. if the limit parameter is set, the returned array contains a maximum of limit elements, and the final element contains the rest of the str string.
If it is a negative number, all elements except the last-limit elements are returned. Note that if the separator is empty, the function returns false, if separator cannot be found in str, an array of elements is returned, which is str.
$ Str1 = "abc @ cde @ aaa @ dec ";
$ Arr = explode ("@", $ str1 );
$ Arr1 = explode ("@", $ str1, 2 );
Print_r ($ arr); // Array ([0] => abc [1] => cde [2] => aaa [3] => dec)
Print_r ($ arr1); // Array ([0] => abc [1] => cde @ aaa @ dec)
?>
9. Merge strings
The string implode (string glue, array pleces) function combines the array pleces with the glue separator.
$ Arr = array ("abc", "cde", "aaa"); // defines an array
Echo implode ("@", $ arr); // abc @ cde @ aaa
?>
Recently I am studying PHP strings. I will take a note to make it easier to remember.