PHP Learning Notes--manipulation of strings

Source: Internet
Author: User
Tags strcmp
Manipulation of strings
1. Remove the leading and trailing spaces and special characters of a string
function trim () to remove characters from the start position and end position
Syntax string trim (String str[, String charlist]), where charlist is an optional parameter that specifies the deletion of the string, carries the words, deletes the specified character, and removes all optional characters if it is not carried.
Optional characters are (null), \ t (tab), \ n (line break), \x0b (vertical tab), \ r (carriage return), "" (space)
The function LTrim () is used to remove the left space or specify a character
Syntax string LTrim (String str[, String charlist]), where charlist is an optional parameter that specifies the deletion of the string, carries the words, deletes the specified character, and removes all optional characters if it is not carried.
Optional characters are (null), \ t (tab), \ n (line break), \x0b (vertical tab), \ r (carriage return), "" (space)
function RTrim () to remove the right space or specify characters
Syntax string LTrim (String str[, String charlist]), where charlist is an optional parameter that specifies the deletion of the string, carries the words, deletes the specified character, and removes all optional characters if it is not carried.
Optional characters are (null), \ t (tab), \ n (line break), \x0b (vertical tab), \ r (carriage return), "" (space)

2. Escape of the string. The
escapes the string, divided into manual escaping and auto-escaping.
Manual escape: Use the escape character "\", such as:
echo "This ' s a \" cat\ ";" This ' s a "cat";
Auto-escape: For simple strings, you can use manual escape, if the data volume comparison of strings, preferably can be automatically escaped, mainly with the following function;
Addslashes () to add a slash "\" to a string;
Syntax string Addslashes (string str);
Stripslashes () is used to restore Addslashes () escaped string, such as
!--? php
$str = "SELECT * from table_student WHERE name = ' Jim '"; The
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: All data is inserted database, it is necessary to escape with the addslashes () function to prevent special characters from escaping, and an error occurred while inserting the database.

Addcslashes () is used to precede the specified character with a backslash;
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 a string
function strlen () Gets the length of the string
$STR = "SELECT * from table_student WHERE name = ' Jim '";
echo strlen ($STR); The length of the Str string is 46
?>

4. Intercepting strings
function string substr (string str, int start[, int length])
Where Str is to intercept the original string, start is where to start cutting, to note that the first bit of the string is 0, the length of the optional parameter is truncated, if not carried, refers to the interception to the last, if negative, refers to the number of characters intercepted to the countdown

$STR = "SELECT * from table_student WHERE name = ' Jim '";
Echo substr ($STR, 2); Do not carry optional parameters length
Echo substr ($STR, 3, 20);//from 3 to 20
Echo substr ($STR, 3,-1);//from 3 to the penultimate character
?>

5. Comparing strings
There are three ways 1. According to Byte 2. According to the natural sorting Method 3. Specify the location of the source string to compare
1). by byte
Use the following two functions 1.strcmp () 2.strasecmp ()
A.STRCMP (String str1, String str2) Comparisons are case-sensitive, and the return is greater than 0,str2 if the 0,STR1 large return is less than 0
B.STRCASECMP (String str1, String str2) is case insensitive
Such as
$STR 1 = "abc";
$str 2 = "ABC";
Echo strcmp ($str 1, $str 2). "
";//Case-sensitive
Echo strcasecmp ($str 1, $str 2). "
";//Case insensitive
?>

2). Sort By nature
In the case of natural sorting, in which the characters in the string are the same, the comparison is usually used to compare the numbers in the string such as STR1,STR2,STR3 ... This type of string
the function int strnatcmp (string str1, String str2) is compared case-sensitive, and the return is greater than 0,str2 if the 0,STR1 large return is less than 0

$str 1 = "ABC2";
$str 2 = "ABC10";
Echo strcmp ($str 1, $str 2). "
";///by Byte ratio, return 1,STR1 large, because in the computer 2:1 large
Echo strnatcmp ($str 1, $str 2). "
*//According to the natural sort, return -1,str2 large, because by the number ratio, 10 is greater than 2
?>

3). Specify the location from the source string
The int strncmp (string str1, str2, int len) function is used to compare the first n characters in a string, such as
$str 1 = "ABCE";
$str 2 = "ABCDF";
Echo strncmp ($str 1, $str 2, 4). "
";//returns 1, compares the first 4 characters of two strings
?>

6. Retrieval of strings
1). function string Strstr (String haystack, string needle)
Gets the string where the needle string first appears in haystack to the end of haystack, if there is a string, returns the substring above, or false if not, note that the function needs to be case-sensitive
$str 1 = "ABCE";
$str 2 = "ABCDF";
$str 3 = "BCD";
Echo strstr ($str 1, $str 2). "
";//return False
Echo Strstr ($str 2, $str 3);//Return BCDF
?>

2). The function int Substr_count (String haystack, string needle) queries needle The number of occurrences of haystack,
!--? php
$str 1 = "abc";
$str 2 = "ABCDFABCCDABC";
$str 3 = "ECA";
Echo Substr_count ($str 2, $str 1). "
";//returns 3
Echo Substr_count ($str 2, $str 3)."
;//return 0
?

7. Replace string
1). Str_ireplace (Mix search, mix replace, mixed subject[, int &count]) find search in subject, and replace search with replace, where &count represents the number of times to replace string execution, it is important to note that the function is case-insensitive, case-sensitive, using str_replace ()
!--? php
Echo str_ Ireplace ("World", "John", "Hello World!world", $i);//hello john! John
Echo $i;//i is 2, which represents a total of 2 replacements
.
2). function Substr_replace (string str, string repl, int start[, int length]) in string s TR, from the start position to the last substitution to the string repl, the optional parameter length indicates a replacement, the empty meaning is from the beginning to the end.
!--? php
$str = "ABCDEFGHIJLJLJLLJLJLJLJLJ";
$repl = "12121231";
Echo substr_replace ($str, $REPL, 2). "
"; The//length parameter is empty, always replaced to the end, ab12121231
Echo substr_replace ($str, $repl, 2, 3);//length is 3, AB12121231FGHIJLJLJLLJLJLJLJLJ
?

8. Splitting a string
The function array explode (string separator, string str[, int limit]) uses the separator delimiter to split str, where limit is an optional parameter, and if limit sets a value, Indicates that the returned array contains a maximum of limit elements, and the last element contains the remainder of the STR string, if the limit
is negative, it returns all elements except the last-limit element, and it is also important to note that if separator is empty, then the function returns False if separator is not found in Str, and returns an array of elements that are STR
$str 1 = "Abc@cde@aaa@dec";
$arr = Explode ("@", $str 1);
$arr 1 = explode ("@", $str 1, 2);
Print_r ($arr);//array ([0] = ABC [1] = CDE [2] = = AAA [3] = dec)
Print_r ($arr 1);//array ([0] = ABC [1] = Cde@aaa@dec)
?>

9. Merging strings
The function string implode (string glue, array pleces) uses the delimiter glue to merge the array pleces.
$arr = Array ("abc", "CDE", "AAA");//define an array
echo implode ("@", $arr);//abc@cde@aaa
?>

Recently in the study of PHP string, summed up, make a note, easy to remember.

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