Syntax
String string can be defined in three ways
' '(single quote) "" (double quote) <<<(delimiter)
Escape sequence Can be interpreted as
The biggest feature of double quotes is that the variable name will be replaced by the variable value
delimiter can be output as is in html format
Print string There are two methods
echo. It is a language structure, not a real function. The difference between it and print is that it can accept multiple parameters.
print syntax bool (Boolean line) print(string agr) secondary function output string if Returns 1 on success and 0 on failure. For example, if the customer's browser suddenly hangs during transmission, it will cause a failure
String processing function
ltrim Syntax format: string ltrim(string str); Delete characters Leading spaces in the string
rtrim Grammar format: string rtrim(string str); Delete the suffix spaces in the string
trim Grammar format: string rtim(string str); Delete the spaces at both ends of the grandfather string
strrev Grammar Format: string strrev(string str); Reverse the string Reverse the string front and back
strtolower Syntax format: string strtolower (string str); Change all strings to lowercase
strtoupper Syntax format: string strtoupper (string str ); Change 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); The first letter of each word is changed to uppercase
Format string for display
sprintf This function formats the string
sprintf() syntax format string sprintf(string format,mixed[args ]...)
Example:
$a=sprintf ("%'*6s","kkk")
echo $a;
Output result: ***kkk
b Convert integer to binary.
c Convert the integer into the corresponding ASCII character.
d Convert integer to decimal place.
f Convert single precision numbers to floating point numbers.
o Convert an integer to octal.
s Convert to string.
x Convert integer to lower case hexadecimal.
X Convert integer to uppercase hexadecimal.
printf outputs a formatted string and returns an integer
Syntax format: int printf(string format, mixed [args]...);
Example
printf("%d--%s- -%x",72,72,72);
Output result: 72--72--48
Split and spell string
explode syntax format: array explode ( string separator, string 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) The return value is an integer. Substring
strcmp sub-string is used to compare the size. Negative number means 1 is less than 2. Positive number means 1 is greater than 2. 0 means equal.
strlen syntax format: int strlen(string str ); Get the length of the string and return the value integer
Substr syntax format: string substr(string string, int start, int [length]);
Get part of the string string is a string, take a few characters from start to stop If stop is omitted, go to the end. If the start is set to a negative number, it means taking
strstr string strstr(string haystack, string needle); returns the beginning of a certain string in the string to the end of the string, that is, starting from somewhere Get the last segment of the string
strrchr for the boundary point. Syntax format: string strrchr(string haystack, string needle); Get the last position of the string. Intercept the string from back to front. If not found, return false.
strpos Syntax format: int strpos(string haystack, string needle, int [offset]); Searching for the first occurrence of the string starts from 0 by default. If not found, return false
strrpos Syntax format: int strrpos(string haystack, char needle); Find the last occurrence of the string. Return false if not found. Return value integer
String padding
str_pad syntax format Can be used to protect some sensitive information
str_pad($input, 10 ); , STR_PAD_BOTH); // produces "__Alien___"
str_pad($input, 6 , "___"); // produces "Alien_"
STR_PAD_LEFT String left padding
STR_PAD_RIGHT String right padded
STR_PAD_BOTH Adding both ends of string
Replacement of string
str_replace() There are three methods of function replacement
str_replace("l","@","hello"); // What to replace What to replace Who to replace
$arr2=array("%color%","%title%","%body%");
$arr3=array("red","hello", "world");
echo str_replace($arr2,$arr3,"%body%");
Touch version Method
String translation
strtr() is an important replacement function also called string translation
strtr also has two calling methods
strtr(string $str, string $from, string $ to)
$arr5=array("hello"=>"hi","hi"=>"hello");
echo strtr("hello is hi",$arr5);
Just translate something into something
strtr(string $str, array $replace_pairs)
html processing
HTML entity processing
htmlentities(); (specify conversion mode and characters Set encoding)
ENT_COMPAT (default): Specifies to convert double quotes but retain single quotes
ENT_QUOTES: Indicates that double quotes and single quotes are converted at the same time
ENT_NOQUOTES: Indicates that neither is converted
htmlspecialchars(); (Convert special symbols such as <> into entities<>)
Syntax format
$str="
China
";