PHP string sorting method

WBOY
Release: 2016-07-25 08:54:04
Original
1084 people have browsed it
This article introduces the method of php string sorting, including accessing single characters, deleting whitespace characters, html escaping and other operations. Friends in need can refer to it.

php implements access to characters in strings, sorting strings, and transcoding strings.

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]. What should be noted here is that if the characters are outside the ASCII code, there will be problems with access. Because this access can only obtain one byte.

2, remove whitespace characters In PHP, you can use trim(), ltrim(), and rtrim() functions to remove 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 are deleted: space (|ox20), tab (n|ox09), line feed (n|oxoa), carriage return (r|0x0d), empty character ( You can also specify it yourself in the parameters.

3, change case strtolower() Converts the entire string to lowercase. strtoupper() Converts the entire string to uppercase. ucfirst() converts the first character of the 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 escaping HTML escaping refers to converting a string into a string for HTML display. In this regard, there are two functions in php to 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 to implement this function. urlencode() and urldecode() convert spaces into + signs and other characters into url strings. The former converts and the latter reversely converts. rawurlencode() and rawurldecode() convert spaces into %20 numbers, which are ordinary url strings, and others into url strings. The former converts and the latter reversely converts

6, sql escape The two databases most related to PHP (mysql and postgresql) both use backslashes as escape characters (oracle defines it by itself, and other databases have not tested it). For this, the addslashes() function is used in PHP to add these backslashes. slashes, use the stripcslashes() function to remove these backslashes.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template