PHP to learn about collating strings

  • 2020-03-31 21:37:19
  • OfStack

1. Access a single character
In PHP, you can treat a string as an array of characters, and you can access the string directly using the array's access methods. Such as $STR [0].
The important thing to note here is that if the characters are outside of ASCII code, access can be problematic. Because this access only gets one byte.

2. Remove white space characters
In PHP, trim(), ltrim(), and rtrim() can be used to remove white space at the beginning or end of a string.
Trim () is used to delete whitespace characters before and after characters. Ltrim () is used to delete the white space character to the left of the character; Rtrim () is used to remove the white space character to the right of the character.
By default, the following characters are deleted: space (|Ox20), TAB(\n|Ox09), newline (\n|OxOA), carriage return (\r|0x0D), empty character (\0|Ox00).
You can also specify it in the parameter itself.
3. Change case
Strtolower () converts the entire string to lowercase.
Strtoupper () converts the entire string to uppercase.
Ucfirst () converts the first character of a string to uppercase, leaving the other characters unchanged.
Ucwords () converts the first character of each word in the string to uppercase, leaving the other characters unchanged.

4. HTML escaped
HTML escape is the conversion of a string to a string for HTML display. There are two functions in PHP that do this.
Htmlentities () converts all convertible characters except Spaces to HTML.
Htmlspecialchars () converts the necessary (ampersand &, double quote, single quote, greater than, less than) into HTML.

5. URL escaped
URL escape is the conversion of a string to a URL string. There are two functions in PHP that do this.
Urlencode () and urldecode() convert Spaces to + and the rest to URL strings, the former to, the latter to back
Rawurlencode () and rawurldecode() convert a space to %20, which is a normal URL string, and the rest to a URL string, the former to convert, the latter to convert

6. SQL escaped
The two databases that are most relevant to PHP (MySQL and PostgreSQL) are both escaped by backslashes (Oracle defines itself, other databases are not tested), for which PHP USES the addslashes() function to add the backslashes and the stripcslashes() function to remove the backslashes.

Reference:
PHP programming, 2003, chapter 4 strings, accessing a single string; Collate string; Code and escape


Related articles: