PHP string operation function set
String sorting: Chop (), ltrim (), and trim ()
The first step to sort strings is to clear unnecessary spaces in strings.
By default, the characters to be removed are linefeeds and carriage returns (N and R), horizontal and vertical tabs (T and x0b), string terminator (0), and spaces. in addition to the default filtering character list, you can also provide the special characters to be filtered in the second parameter of the function.
The difference between the three functions is that trim () removes spaces before and after the string, while ltrim () removes spaces only from the start of the string (left. rtrim () removes spaces from the end of the character.
Format the string for display
1. Use HTML Format: nl2br () function
The nl2br () function uses a string as the input parameter and uses
Mark to replace the linefeed in the string.
2. format the string to print the output
PHP also supports the print () structure, which has the same functions as Echo.
The printf () function outputs a formatted string to the browser, while the sprintf () function returns a formatted string.
See the following:
Printf ("total amount of order is % S.", $ total );
% S in the formatted string is the conversion description. It means "replacing with a string ".
To print a "%" symbol, you must use "% ".
Let's look at another example:
Printf ("total amount of order is %. 2f (with shipping %. 2f)", $ total, $ total_shipping );
Type Code of conversion description
B is interpreted as an integer and output as a binary number.
C is interpreted as an integer and output as a character.
D is interpreted as an integer and output as a decimal number.
F is interpreted as double precision and output as a floating point number.
O is interpreted as an integer and output as an octal value.
S is interpreted as a string and output as a string
U is interpreted as an integer and output as an unspecified decimal number.
X is interpreted as an integer and output as a hexadecimal number with a lowercase letter a-f.
X is interpreted as an integer and output as a hexadecimal number with an upper-case A-F
3. Change the uppercase and lowercase letters in the string.
Strtoupper () converts string to uppercase
Strtolower () converts string to lowercase
Ucfirst () if the first character of a string is a letter, it is converted to uppercase.
Ucwords () converts the first letter of each word in a string to uppercase.
Format the string for storage: addslashes (), stripslashes ()
Some data stored in the database may be interpreted as control symbols. The problematic characters include quotation marks (single and double quotes), backslash (), and null characters.
To escape these characters, you can add a backslash before them.
When storing data, use addslashes () to process the string and add a backslash. Before displaying user data, use stripslashes () to remove the quotation marks.
PHP configuration can also automatically start the magic quotes feature, which is controlled by the magic_quotes_gpc command.
Concatenate and split strings using string functions
Use the functions explode (), implode () and join ()
For example:
$ Email_array = explode ('@', $ email );
Here, the customer's email is divided into two parts. Stored in $ email_array [0] and $ email_array [1].
However, this function cannot separate strings with uppercase letters. Therefore, it should be converted to lowercase before processing.
Use implode () and join () functions to achieve the opposite effect of explode (0. These two functions have the same effect.
Use the strtok () function
Unlike explode (), each time a string is divided into several small pieces, the strtok () function only extracts some fragments from the string at a time (as tokens ). strtok () is better for extracting a word from a string at a time.
Use the substr () function
The substr () function allows us to access a string with a given start point and end point.
For example:
$ Test = 'your customer service is excellent ';
Substr ($ test, 1 );
Will return: "our customer service is excellent"
Substr ($ test,-9 );
Will return: "excellent"
The 3rd parameters of this function can limit the size of the returned characters:
Substr ($ test, 5,-13 );
Will return: "Customer Service"
String comparison
String sorting: strcmp (), strcasecmp (), and strnatcmp ()
Function prototype:
Int strcmp (string str1, string str2 );
This function requires two parameter strings for comparison. if the two strings are equal, the function returns 0. If the lexicographically ordered str1 is behind str2 (greater than str2), a positive number is returned. otherwise, a negative number is returned. this function is case sensitive.
The strcasecmp () function is the same as strcmp () except case insensitive.
The strnatcmp () function and its corresponding case-insensitive strnatcasecmp () function compare strings by "natural sorting.
The so-called "natural sorting" is in the customary order. For example, strcmp () considers that 2 is greater than 12 because 2 is greater than 12 in the dictionary order. However, strnatcmp () is the opposite.
Use the strlen () function to test the string length.
Use string functions to match and replace substrings
Search for strings: strstr (), strchr (), strrchr (), and stristr ().
For example:
If (strstr ($ feedback, 'shop '))
$ Toaddress = 'shop @ example.com ';
Else if (strstr ($ feedback, 'delivery '))
$ Toaddress = 'delivery @ example.com ';
The strstr function has two variants: stristr (), which is case insensitive. the second is strrchr (), which is almost the same as strstr (), but strstr returns a string from the front of the first position where the needle appears, while strrchr () is returned from the position of the last needle.
Locate the substring: strpos (), strrpos ()
See the followingCode, The value 4 is displayed in the browser:
$ Test = 'Hello world ';
Echo strpos ($ test, 'O ');
Echo strpos ($ test, 'O', 5 );
7 is returned, because the third parameter indicates that the search starts at Location 5.
Strrpos () is almost the same, but it returns the position from the last position of the sub-string needle.
Replace the substring: str_replace (), substr_replace ()
Str_replace () is the most common one. Its prototype is:
Mixed str_replace (mixed needle, mixed new_needle, mixed haystack [, Int & COUNT]);
This function replaces needle in all haystack with new_needle and returns the result after replacement. The fourth optional parameter is count, which contains the number of replacement operations to be executed.
For example, the statement used to filter the word "emotional color:
$ Feedback = str_replace ($ offcolor, '%! @ ', $ Feedback );
Prototype of substr_replace:
String substr_replace (string, string replacement, int start, int [length]);
This function uses the string replacement to replace a part of the string. The specific part depends on the start position and the value of the optional parameter lengeh.
$ Test = substr_replace ($ test, 'x',-1 );
This code replaces the last character of $ test with 'X.