Home > Article > Backend Development > PHP learning to organize strings_PHP tutorial
1. Access a single character
In PHP, you can treat a string as an array of characters, and you can directly access the string using the array access method. Such as $str[0].
It should be noted here that if the characters are other than ASCII codes, there will be problems with access. Because this access can only obtain one byte.
2. Delete whitespace characters
In PHP, you can use trim(), ltrim(), rtrim() three functions to delete whitespace characters at the beginning or end of a string.
Among them, trim() is used to delete the blank characters before and after the character; ltrim() is used to delete the blank characters on the left side of the character; rtrim() is used to delete the blank characters on the right side of the character.
By default, the following characters will be deleted: space (|Ox20), tab character TAB (n|Ox09), line feed (n|OxOA), carriage return (r|0x0D), empty character (
4. HTML escaping HTML escaping refers to converting a string into a string for HTML display. In this regard, there are two functions in PHP that implement this function.
htmlentities() converts all convertible characters except spaces into HTML form.
htmlspecialchars() converts the necessary (ampersands &, double quotes, single quotes, greater than sign, less than sign) into HTML form.
5. URL escaping
URL escaping refers to converting a string into a URL string. In this regard, there are two functions in PHP that implement this function. urlencode() and urldecode() convert spaces into + signs, and others into URL strings. The former converts and the latter reverse conversion.
rawurlencode() and rawurldecode() convert spaces into %20 number, that is, ordinary URL strings, and others are converted into URL strings. The former is converted and the latter is reversely converted.
6. SQL escape
The two databases most related to PHP (MySQL and PostgreSQL) both use backslash as the escape character (Oracle defines it by itself, other databases do not Test), use the addslashes() function in PHP to add these backslashes, and use the stripcslashes() function to remove these backslashes.
Reference materials:
PHP Programming, 2003, Chapter 4 Strings, accessing a single string; organizing strings; encoding and escaping
http://www.bkjia.com/PHPjc/323156.html
www.bkjia.com
http: //www.bkjia.com/PHPjc/323156.html