Generally, website background programs must format these strings before using the strings entered by users (generally submitted through HTML forms) to conform to program running standards, prevents errors and vulnerabilities. In the following content, we will introduce some available PHP functions to implement this process.
Generally, website background programs must format these strings before using the strings entered by users (generally submitted through HTML forms) to conform to program running standards, prevents errors and vulnerabilities. In the following content, we will introduce some available PHP functions to implement this process.
String sorting
The first step of string sorting is to clear unnecessary spaces or other redundant characters in the string. Of course, this step is not required if you do not store a string in a file or database, or do not compare it with other strings. Therefore, we can organize it flexibly as needed.
To implement the string sorting function, PHP provides three very useful functions. At the beginning of receiving and submitting a string script, you can use the trim () function to sort user input data when you want to enter a variable in the form to define a brief variable name, the usage is as follows:
$ Username = trim ($ _ POST ['username']);
$ Password = trim ($ _ POST ['password']);
$ Email = trim ($ _ POST ['email ']);
The trim () function removes spaces at the start and end positions of the string and returns the result string. By default, the characters to be removed are linefeeds and carriage returns (n and r), horizontal and vertical tabs (t and x0B), string terminator (), and spaces. In addition to these default filter strings, you can also provide other special characters to be filtered in the second parameter of the function. The ltrim () and rtrim () functions may be used for specific purposes.
The functions of these two functions are similar to trim () functions. they can both use strings to be processed as input parameters and then return formatted strings. The difference is that the trim () function removes spaces before and after the entire string, and ltrim () removes spaces from the start of the string (left), rtrim () removes spaces from the end (right) of the string.
Format a string
PHP provides a series of functions that can be used to reformat strings. these functions work in different ways and can be used in multiple places of the program. developers can use them flexibly.
1. nl2br () function
The nl2br () function uses a string as the input parameter and uses
The line break that marks the string. This is very advantageous for displaying a long string in the browser. In general, HTML ignores pure spaces, so if you do not use the nl2br () function to filter the output results, it looks like a separate line (unless the browser window is forced to wrap ).
2. print () function
Generally, we are used to outputting strings using echo statements. in PHP, the print () structure is also supported. it implements the same functions as echo. The only difference is that it has a return value: true or false, the output is successful or failed. In addition to print (), you can also use printf () and sprintf () to implement some complex formats. They work in the same way, but printf () outputs a formatted string to the browser, and sprintf () returns a formatted string.
3. change the uppercase and lowercase letters in the string.
Sometimes you need to format the uppercase and lowercase letters of the input. PHP provides some functions to change the uppercase and lowercase letters in the string. these functions and their functions are as follows:
Strtoupper (): converts letters in a string to uppercase letters, strtolower (): converts letters in a string to lowercase letters, ucfirst (): If the first character of a string is a letter, this character is converted to uppercase, and the first letter of each word in the string is converted to uppercase.
Format strings for storage
Some characters must be valid, but some problems may occur when data is inserted into the database, because the database interprets these characters as controllers. These problematic characters are quotation marks (single and double quotes), backslash (), and NULL characters.
PHP provides two functions specifically used to escape strings. Before writing any strings to the database, if your default PHP configuration has not enabled this function, you should use addslashes () to reformat them, for example:
$ Post_content = addslashes (trim ($ _ POST ['post _ content']);
Like other string formatting functions, addslashes () requires a string as the transfer parameter. after processing by this function, a re-formatted string is returned.
The automatic addition or removal of escape characters is controlled by the magic_quotes_gpc configuration command of PHP. This command is enabled by default when the new version of PHP is installed. Gpc indicates GET, POST, and COOKIE. it is a combination of letters. This means that variables from these methods or methods will be automatically included in quotation marks. Use the get_magic_quotes_gpc () function to check whether this command is enabled in the system. If enabled, you must call the stripslashes () function before displaying user data. otherwise, escape characters are displayed.