Remove string leading and trailing spaces and special characters
When users enter data, they often inadvertently enter extra spaces, and in some cases, spaces and special characters are not allowed in the string, and spaces and special characters in the string need to be removed. The trim () function is provided in PHP to remove whitespace and special characters from the left and right sides of the string, LTrim () function to strip the left space and special characters of the string, and the RTrim () function to remove the whitespace and special characters from the string.
1. Trim () function
The trim () function is used to remove spaces at the beginning and end of a string, and returns a string after the space is stripped.
The syntax format is as follows:
String Trim (String str [, String charlist]);
The parameters of the trim () function str is the string object to manipulate, the parameter charlist is an optional parameter, specifies which characters need to be removed from the specified string, and if the argument is not set, all the optional characters are deleted.
The optional values for parameter charlist of the trim () function are shown in the following table.
Parameter values |
Description |
/ |
NULL, NULL value |
\ t |
tab, tab |
\ n |
Line break |
\x0b |
Vertical tab |
\ r |
Carriage return character |
" " |
Space |
Note: In addition to the above default filter character list characters, you can also provide special characters to filter in the charlist parameter.
2. Itrim () function
The Itrim () function is used to remove spaces to the left of a string or to specify a string.
The syntax format is as follows:
String LTrim (String str [, String charlist]);
Escaping, restoring string data
There are two ways to escape and restore a string: one is to manually escape, restore the string data, and the other is to automatically escape and restore the string data. These two methods are explained in detail below.
1. Manually escaping, restoring string data
The string can be defined in single quotation marks ('), double quotation marks (""), and delimiter ({}) 3 methods. The simplest way to specify a simple string is to enclose it in single quotation marks ('). When using a string, it is likely that there are characters in the string that are confused with the PHP script, so you must make an escape statement. This will use the escape symbol "\" in front of it.
"\" is an escape character, and the first characters immediately following "\" will become meaningless or have special meaning. such as ' is the delimiter of the string, write as \ ' when the meaning of the delimiter lost, into a normal single quotation mark '. The reader can use echo ' \ '; output a single quote ', while the escape character ' \ ' is not displayed.
Tip 1: If you want to represent single quotes in a string, you need to escape with a backslash (\). For example, to represent the string "I ' m", you need to write "I\ ' m".
Tip 2: It is recommended to use manual methods for string escaping for simple strings, and for strings with large data volumes, it is recommended to use the automatic escape function to implement the escape of strings.
Description: A manual escape string can be used to restore a string using the Addcslashes () function, and its implementation is described below.
2. Automatically escapes, restores string data
Automatically escaping and restoring string data can be implemented using PHP's Addslashes () function and the stripslashes () function.
1.addslashes () function
The Addslashes () function is used to add a slash "\" to the string str.
The syntax format is as follows:
String addslashes (String str)
2.stripslashes () function
The Stripslashes () function is used to return the string after the use of the addslashes () function to Str.
The syntax format is as follows:
String stripslashes (String str);
Tip: All data before inserting the database, it is necessary to apply the Addslashes () function for string escaping, so that special characters are not escaped without escaping an error when inserting the database. In addition, automatic escape strings implemented using the Addslashes () function can be restored using the stripcslashes () function, but the data must be escaped again before inserting the database.
The above two functions implement automatic escaping and restoring of the specified string. In addition to the methods described above, you can limit the range of strings you want to escape, restore, and automatically escape and restore strings within a specified range by using the Addcslashes () function and the stripcslashes () function. The following is a detailed description of two functions.
3.addcslashes () function
Implements the character in the escaped string, which is preceded by a backslash before the specified character charlist.
The syntax format is as follows:
String addcslashes (String str, string charlist)
Parameter description:
The parameter str is the string to be manipulated, and the parameter charlist specifies which characters in the string precede the backslash "\", if the parameter charlist contains \ n, \ r, and so on, it will be converted in C language style. Other non-alphanumeric and ASCII codes less than 32 and more than 126 characters nonspacing converted to octal notation.
Note: When defining the range of parameter charlist, you need to explicitly specify the characters in the start and end of the range.
4.stripcslashes () function
The Stripcslashes () function is used to restore the string str that is escaped by applying the addcslashes () function.
The syntax format is as follows:
String stripcslashes (String str)
Getting the length of the string is using the strlen () function, which focuses on the syntax of the strlen () function and its application.
The strlen () function is primarily used to get the length of the specified string str.
The syntax format is as follows:
int strlen (String str)
The strlen () function can also be used to detect the length of a string, while getting the length of the string.
Compare strings--Comparison of strings by byte
1. Comparison of strings by byte
There are two methods for string comparisons by Byte, namely the strcmp () and strcasecmp () functions, which enable you to compare bytes by byte. The difference between these two functions is that the strcmp () function distinguishes the case of characters, whereas the strcasecmp () function does not differentiate between the case of characters.
The syntax format for the strcmp () function is as follows:
int strcmp (String str1, String str2)
The parameter str1 and parameter str2 specify the two strings to compare. Returns 0 if equal, if the parameter str1 is greater than str2, and the return value is greater than 0 if the parameter str1 is less than the parameter str2 the return value is less than 0.
2. Comparison of strings by natural sorting method
In PHP, the comparison of strings by natural sorting is done through the strnatcmp () function. The natural sorting method compares the numeric parts of a string, sorting the numbers in the string by size.
The syntax format is as follows:
int strnatcmp (String str1, String str2)
Returns 0 if the string is equal, if the parameter str1 is greater than the parameter str2 the return value is greater than 0, and the return value is less than 0 if the parameter str1 is less than the parameter str2. This function distinguishes between uppercase and lowercase letters.
Note: In the natural algorithm, 2:10 is small, and in the computer sequence, 10:2 is small, because the first number in "10" is "1", which is less than 2.
Retrieving strings
1. Use the STRSTR () function to find the specified keyword
Gets a substring that specifies the position of the first occurrence of a string in another string to the end of the latter. If successful, returns the remaining string (there is a matching character), or false if no matching character is found.
The syntax format is as follows:
String Strstr (String haystack, string needle)
Parameter description:
Haystack the necessary parameters, specifying which string to search from needle the necessary parameters, specifying the object to search. If the parameter is a numeric value, the character that matches the ASCII value of this value is searched
2. Use the Substr_count () function to retrieve the number of occurrences of a substring to get the number of occurrences of a specified character in a string.
The syntax format is as follows:
The int substr_count (string haystack,string needle) parameter haystack is the specified string, and the parameter needle is the specified character.
Replace string
Replacement of the specified character in the specified string can be accomplished by the substitution technique of the string. The substitution technique for strings can be implemented with the following two functions: the Str_ireplace () function and the Substr_replace () function.
1. Str_ireplace () function
Replaces the string syntax format specified in the original string with the new substring (substring) as follows:
Mixed Str_ireplace (mixed search, mixed replace, mixed
subject [, int &count])
Replace all parameters that appear in parameter subject with the parameter replace, and the parameter &count represents the number of times the string is substituted for execution. This function is case-sensitive. Parameter description: Search necessary parameters, specify the string to find, replace the necessary parameters, specify the replacement value, subject the necessary parameters, specify the scope of the lookup; count optional parameters, get the number of execution substitutions
formatting strings
In PHP, the format of the string has a variety of methods, according to the format of the type can be divided into the format of the string and the number of formatting, the number of formatting is most commonly used, this section will focus on the number format Number_format () function. The Number_format () function is used to format a numeric string.
The syntax format is as follows:
String Number_format (float number,[int
Num_decimal_places],[string dec_seperator],string
Thousands_ seperator)
The Number_format () function can have one, two, or 4 parameters, but not 3 parameters. If only one parameter number,number the value after the decimal point, and every 1000 will be separated by commas (,), if there are two parameters, number is formatted to the decimal num_decimal_places bit, and every 1000 will be separated by commas If there are 4 parameter number formatted to the decimal num_decimal_places bit, dec_seperator is used instead of the decimal point (.), Thousands_seperator is used to replace each comma separated by 1000 (,)
Split string
The segmentation of a string is achieved through the explode () function. The explode () function splits a string by the specified rules and returns an array of values.
The syntax format is as follows:
Array explode (string separator,string str,[int limit])
Composite string
The implode () function can combine the contents of an array into a new string.
The syntax format is as follows:
String implode (string glue, array pieces)
Parameter description:
The parameter glue is a string type that specifies the delimiter, and the parameter pieces is an array type that specifies the array to be merged
| GetType |
Gets the type of the variable. |
| Intval: |
The variable is converted to an integer type. |
| Doubleval: |
Variable to multiply the number of floating point types. |
| Empty |
Determines whether the variable is configured. |
| Is_array: |
Determines whether the variable type is an array type. |
| Is_double: |
Determines whether the variable type is a double floating-point number type. |
| Is_float: |
Determines whether the variable type is a floating-point number type. |
| Is_int: |
Determines whether the variable type is an integer type. |
| Is_integer: |
Determines whether the variable type is a long integer type. |
| Is_long: |
Determines whether the variable type is a long integer type. |
| Is_object: |
Determines whether the variable type is a class type. |
| Is_real: |
Determines whether the variable type is a real type. |
| Is_string: |
Determines whether the variable type is a string type. |
| Isset |
Determines whether the variable is configured. |
| Settype |
Configures the variable type. |
| Strval: |
Converts a variable into a string type. |
| unset |
Delete the variable. |
Remove string leading and trailing spaces and special characters