Php learning string courseware

Source: Internet
Author: User
Tags alphanumeric characters
String definition string (string) the characters in PHP are the same as those in bytes. it has a syntax of 256 characters.
String can be defined in three methods
''(Single quotes)" "(double quotes) <(delimiter)

Escape sequences can be interpreted

The biggest feature of double quotation marks is that the variable name will be replaced by the variable value

The delimiters can output html format as they are.


You can print strings in either of the following ways:
Echo is a language structure rather than a real function. The difference between echo and print is that it can accept multiple parameters.
Print syntax bool (Boolean row) print (string agr) function output string if successful return 1 failed return 0 columns if the client's browser suddenly fails during transmission will cause failure

String processing functions
Ltrim syntax format: string ltrim (string str); delete leading spaces of a string
Rtrim syntax format: string rtrim (string str); delete the suffix space of the string
Trim syntax format: string rtim (string str); delete spaces at both ends of grandfather wear
Strrev syntax format: string strrev (string str); reverse a string to reverse the string
Strtolower syntax format: string strtolower (string str); converts all strings to lowercase letters.
Strtoupper syntax format: string strtoupper (string str); converts all strings to uppercase
Ucfirst syntax format: string ucfirst (string str); change the first letter of the string to uppercase
Ucworde syntax format: string ucworde (string str); change the first letter of each character in the string to uppercase.

Format the string for display
Sprintf this function formats strings
Sprintf () syntax format string sprintf (string format, mixed [args]...)
Example:
$ A = sprintf ("% '* 6 s", "kkk ")
Echo $;
Output result: *** kkk
The integer B is converted into binary.
The c integer is converted to the corresponding ASCII character.
D integer to decimal place.
F returns a single precision number to a floating point number.
O integer to octal.
S is converted into a string.
Convert an integer to a lowercase hexadecimal value.
Convert X to uppercase hexadecimal.
Printf outputs the integer returned by the formatted string
Syntax format: int printf (string format, mixed [args]...);
Example
Printf ("% d -- % s -- % x", 72, 72 );
Output result :-

Split and spell string
Explode syntax format: array explode (string separator, string [, int limit])
Implode syntax format: string implode (shring glue, array pieces) concatenates the array function into a string

String comparison
Strcmp syntax format: int strcmp (string 1, string 2) returns an integer substring.
The strcmp string is used to compare negative values. 1 is smaller than 2. 1 is greater than 2. 0 is equal.
Strlen syntax format: int strlen (string str); returns an integer from the string length.
Substr syntax format: string substr (string, int start, int [length]);
Obtain some strings. the string is the start character of the string and the number of characters to be stopped. if the character is omitted, the string is to the end. if it is set to a negative number, the string is obtained from the back image.
Strstr string strstr (string haystack, string needle); returns the string from the start to the end of a string. This means that the string of the last segment is obtained from a certain point.
Syntax format: string strrchr (string haystack, string needle)
Strpos syntax format: int strpos (string haystack, string needle, int [offset]); returns false if no value is found when the first position of the string appears.
Strrpos syntax format: int strrpos (string haystack, char needle); returns an integer that returns false if the final position of the string is not found.

String filling

The str_pad syntax format can be used to protect sensitive information.
Str_pad ($ input, 10); // produces "Alien"
Str_pad ($ input, 10, "-=", STR_PAD_LEFT); // produces "-=-Alien"
Str_pad ($ input, 10, "_", STR_PAD_BOTH); // produces "_ Alien ___"
Str_pad ($ input, 6, "___"); // produces "Alien _"
STR_PAD_LEFT string left complement
Add the STR_PAD_RIGHT string to the right
Supplement the two ends of the STR_PAD_BOTH string

String replacement
Str_replace () function replacement
There are three methods
Str_replace ("l", "@", "hello"); // replace
$ Arr2 = array ("% color %", "% title %", "% body % ");
$ Arr3 = array ("red", "hello", "world ");
Echo str_replace ($ arr2, $ arr3, "% body % ");
Touch version method

String translation
An important replacement function of strtr () is also called string translation.
Strtr can also be called in two ways.
Strtr (string $ str, string $ from, string $)
$ Arr5 = array ("hello" => "hi", "hi" => "hello ");
Echo strtr ("hello is hi", $ arr5 );
Is to translate
Strtr (string $ str, array $ replace_pairs)


Html processing
Processing HTML objects
Htmlentities (); (specify the conversion mode and character set encoding)
ENT_COMPAT (default): specifies to convert double quotation marks but keep single quotation marks
ENT_QUOTES: double quotation marks and single quotation marks are converted simultaneously.
ENT_NOQUOTES: indicates that neither of them is converted.
Htmlspecialchars (); (converts special symbols such as <> into entities <>)
Syntax format
$ Str ="

China

";
Echo htmlentities ($ str); // Convert the encoding
Echo htmlentities ($ str, ENT_QUOTES, "EUC-JP"); // double quotation marks are converted to the character encoding
Quotation mark processing
Stripslashes (); (it is the inverse function of addslashes, displaying the characters after transfer)
Addslashes (); (add \ display four characters before '"\ NULL (\ 0)
HTMl tag cleanup
Strip_tags (); (clear HTML hypertext characters in strings)
Url processing
In addition to HTML text processing, the URL address must be processed during WEB development. Processing URLs mainly include:
URL string parsing
Parse_str (); (parses the request string into an array)
Parse_url (); (parses the complete URL into an array)
URL encoding
Rawurlencode (); (replace all non-alphanumeric characters with % followed by two hexadecimal characters)
Urlencode (); (replace all non-alphanumeric characters with % followed by two hexadecimal characters, and the space is changed to +)
Rawurldecode (); (parses and restores URLs encoded in %)
Urldecode (); (parses and restores the URL encoded with %)
Construct a query string.
Http_build_query (); (The request string generated after url-encoded)
Php# .rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.