PHP Learning String Courseware _php Basics

Source: Internet
Author: User
Tags character set explode html tags sprintf strcmp string format strlen alphanumeric characters

Grammar
String strings can have three methods of defining
' (single quotes) "(double quotes) <<< (delimiters)

The escape sequence can be explained by \ \ \ \

The biggest feature of double quotes is that variable names are replaced with variable values.

Delimiters can output HTML format as-is


There are two ways of printing a string
Echo, which belongs to the language structure, is not a real function, and it differs from print in that it can accept multiple parameters
Print syntax bool (Bourg) print (string AGR) Secondary function output string if a successful return of 1 fails to return 0 columns if the client's browser suddenly hangs off, it will cause a failure scenario

String handler function
LTrim syntax Format: string LTrim (String str); Remove leading blanks from a string
RTrim syntax Format: string RTrim (String str); Remove a suffix space for a string
Trim syntax Format: string Rtim (String str); Remove the two-sided space worn by grandfather
Strrev syntax Format: string Strrev (String str); Reverse string to reverse back and forth
Strtolower syntax Format: string strtolower (String str); Turn all strings to lowercase
Strtoupper syntax Format: string strtoupper (String str); Capitalize all strings into uppercase
Ucfirst syntax Format: string Ucfirst (String str); Change the first letter of a string to uppercase
Ucworde syntax Format: string Ucworde (String str); Converts the first letter of each word in a string to uppercase

format string to display
sprintf this function to format strings
sprintf () syntax format string sprintf (String Format,mixed[args] ...)
Example:
$a =sprintf ("% ' *6s", "KKK")
echo $a;
Output Result: ***KKK
The b integer is converted into binary.
The c integer is converted to the corresponding ASCII character.
The d integer is turned into 10.
F single-octave precision numbers are converted to floating points.
The o integer is converted into octal.
s turns into a string.
The x integer is converted to lowercase 16.
The X integer is converted to uppercase 16.
printf output formatted string return value integer
Syntax format: int printf (string format, mixed [args] ...);
Example
printf ("%d--%s--%x", 72,72,72);
Output Result: 72--72--48

Split and Scrabble strings
Explode syntax format: array explode (string separator, string string [, int limit])
Implode syntax Format: string implode (shring glue,array pieces) to concatenate array functions into strings

String comparisons
STRCMP syntax format: int strcmp (String 1,string 2) returns the value as an integer substring
strcmp the second string is used to compare the size negative indicates that 1 is less than 2 positive indicates that 1 is greater than 2 0 to indicate equality
strlen syntax format: int strlen (string str); Get string length return value integer
SUBSTR syntax Format: string substr (string string, int start, int [length]);
Get partial string strings to start bit to stop Fujo omit stop go to tail if start set to negative sign from the back like before
Strstr string Strstr (String haystack, string needle); Returns a string from the string to the end of the string that means that the back segment string is taken somewhere.
STRRCHR syntax Format: string STRRCHR (String haystack, string needle); Gets the last occurrence of the string from the backward forward intercept string not found returns false
STRPOS syntax format: int strpos (String haystack, string needle, int [offset]); Find the first occurrence of the string default starting from 0 if you cannot find the return false
STRRPOS syntax format: int strrpos (String haystack, char needle); Finding where the last occurrence of the string could not find return false return value integer

String fill

Str_pad syntax format can be used for the protection of some sensitive information
Str_pad ($input, 10); Produces "Alien"
Str_pad ($input, ten, "=", str_pad_left); Produces "-=-=-alien"
Str_pad ($input, Ten, "_", Str_pad_both); Produces "__alien___"
Str_pad ($input, 6, "___"); Produces "Alien_"
Str_pad_left string left to replenish
Str_pad_right string Right to replenish
Str_pad_both string at both ends

Substitution of strings
Str_replace () function substitution
There are three different ways
Str_replace ("L", "@", "hello"); Replace what with what replace replace who
$arr 2=array ("%color%", "%title%", "%body%");
$arr 3=array ("Red", "Hello", "World");
Echo Str_replace ($arr 2, $arr 3, "<font color=%color% title=%title%>%body%</font>");
Touch version of the method

String translation
STRTR () An important substitution function is also called string translation
STRTR also has two ways of calling
STRTR (String $str, String $from, String $to)
$arr 5=array ("Hello" => "Hi", "hi" => "Hello");
echo strtr ("Hello is hi", $arr 5);
is to translate what into what
STRTR (string $str, array $replace _pairs)


HTML processing
Processing of HTML entities
Htmlentities (); (Specify conversion mode and character set encoding)
Ent_compat (default): Specify to convert double quotes but keep single quotes
Ent_quotes: represents simultaneous conversion of double and single quotes
Ent_noquotes: means two are not converted
Htmlspecialchars (); (Convert <> and other special symbols to entity <>)
Syntax format
$str = "<p title= ' HHH ' > China </p>";
echo htmlentities ($STR); Conversion encoding
Echo htmlentities ($str, Ent_quotes, "EUC-JP");//single quotation mark double quotation marks convert the code that follows the word
Handling of quotation marks
Stripslashes (); (is the inverse of the addslashes, showing the transferred character)
Addslashes (); (add ' "\ NULL (+) 4 characters before \ Show)
Cleanup of HTML tags
Strip_tags (); (Cleans up HTML hypertext characters in a string)
URL Handling
In addition to the processing of HTML text, the URL address is also processed during web development. Processing URLs include:
Parsing a URL string
Parse_str (); (Parse request string transform array)
Parse_url () (Parse complete URL transition array)
URL encoding Processing
Rawurlencode (); (Replaces all non-alphanumeric characters and becomes% followed by two-bit 16-digit)
UrlEncode (); (Replaces all non-alphanumeric characters, and becomes% followed by two-bit 16, and the spaces become + numbers)
Rawurldecode (); (resolution of a URL that is already% encoded)
UrlDecode (); (to% # #编码的URL进行解析还原)
Constructs a query string, and so on.
Http_build_query (); (the request string after the url-encoded is generated)
PHP string. 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.