1. String Type characteristics
1) because PHP is a weakly typed language, variables that are common to other data types can also be handled by string-related functions.
For example:
SUBSTR: string intercept function
Substr ("String to process", starting value, number of intercepts )
if it's plastic, because PHP is a weakly typed language, so automatic type conversions are converted to string types
2) The string can be treated as an array
As follows:
It is important to note that although the string is treated as an array, it is not a true array
As follows: When dealing with array-related functions, it is not possible to get the correct results
Because the string is considered an array, but it is not a true array, the above array function is invalid, and if you want to measure the length of the string, you have to use the string correlation function.
To differentiate between strings and arrays, you can use curly braces (curly braces) "{}" to make it simple and straightforward
3) If you assign a value to a value in a string, the string value is changed and the string length is constant.
If you assign a string to a character in another string, then only the first character is assigned.
4) about single and double quotation marks
Single quotes: Do not parse variables
Double quotes: Parses a variable
If it is a variable in double quotes, it is best to enclose the variable in a pair of flowers with an arc of "{}" for brevity and clarity.
As follows:
In addition to parsing variables, you can parse arrays and objects in double quotes
2. String-related functions
1) String Output correlation function
Echo (), print (), die (), printf (), Sprint ()
Echo: used to output one or more strings, note that echo can output multiple strings, separated by commas
If there is more than one parameter output, the efficiency of using commas is higher than the point
Echo strictly speaking, is not a function, in fact it is a language structure.
The so-called language structure is the key word. The execution efficiency of a language structure is higher than that of a function, because the execution of a function is ultimately translated into a language structure.
Print (): The function and echo are basically the same, the difference is that print () is a function, and echo is a language structure, So the print execution efficiency is not echo High, and a little bit,print () has a return value, successfully returned 1 , failed to return 0
Die ()
The Die() function is an alias function of the exit () function, and the function and exit () are identical. If you pass a string to the function, the message appears when you exit the script
Printf (), Print, output
this looks familiar, in C language has seen, the statement is directly from the C language directly from the reference.
Sprintf (): exactly the same as printf, except that it does not output, but returns the value in the form of a return value
2) commonly used string formatting functions
The so-called string formatting, refers to the format of the string as you want.
Remove whitespace function
Ltrim (): Remove left space
Rtrim (): Remove right space
Trim (): remove space on both sides , note
This function is used most when the user submits the form, for example, the user registered account is not careful to press the space, if not removed, the next time no matter how can not log in.
This set of functions does not change the original string, but returns the processing result as a return-worthy form.
Trim () This set of functions can define which characters to remove. For example 0..9 represents A filter number,A. Z Filter Capital Letter A. Z Filter the lowercase letters dot. "The representation is the point within the filter string ." ”
In addition,trim also supports custom filter ranges
3) string fill function
Str_pad(string to fill, specified length, padding character, orientation)
direction Default Support 3 :str_pad_left,str_pad_right,str_pad_both
Does not change the length of the original array
4) string case Conversion
PHP provides 4 of the string case conversion function
Strtoupper (): Turn all capitals
Strtolower (): Turn all lowercase
Ucfirst (): the initials of the first word in a string converted to uppercase UC (upper case) lowercase
Ucwords (): The first letter of each word is converted to uppercase
5) string formatting functions related to HTML tags
NL2BR (string name) function:
This function can convert \ n to BR, and many programmers because they used to write C ,theC language inside \ n is a newline, so in order to cater to their habits, there is a special function to convert \ n to BR
Htmlspecialchars (): Converts pre-defined characters ( mainly HTML Tags ) to HTML Entities
< convert to <
> Convert to >
You can let the code output directly, without the browser explaining it, to prevent other people from committing HTML code attacks.
There is also a strip_tags (the string to be processed, the label to keep ): You can delete the HTML tag inside the string directly
Strip_tags (the string to be processed, the label to keep )
Other related functions
Strrev (): Flips all characters inside the string
Number_format(): Standard currency Format function
Number_format($num, You can choose how many decimal places to keep)
Number_format($num, You can choose how many decimal places, separators of decimal places, delimiters for integer digits)
MD5 () function: A string is MD5 Encrypted, returning a hexadecimal string of four digits by default
6) string comparison function
in the PHP can be used to compare strings (==,<,>)
In addition to comparison operators to compare strings, PHP also provides a set of string comparison functions for string comparisons.
STRCMP (STR1,STR2): It's the same as the C language .
STRCASECMP (STR1,STR2): Ignore string case for comparison
String comparisons by natural sorting
"4" "33"
STRNATCMP()
Segmentation of strings
Regular:
Preg_split (M,n) m represents a regular expression of what is Split, n represents the string name being split
Explode (delimiter, string variable name) splits a string into an array
Implode (): Merges an array into a single string
Implode (connection symbol, array)
PHP string processing