Home  >  Article  >  Backend Development  >  PHP Learning String Courseware_PHP Tutorial

PHP Learning String Courseware_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:51:55761browse


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

";
echo htmlentities($str); //Convert encoding
echo htmlentities($str,ENT_QUOTES,"EUC-JP");//Convert single and double quotes, followed by the encoding of the word
Processing of quotation marks
stripslashes(); (is the inverse function of addslashes, showing the transferred characters)
addslashes(); (put ' ” NULL(Processing URLs mainly includes:
Parsing of URL strings
parse_str(); (parse the request string and convert it into an array)
parse_url(); (parse the complete URL and convert it into an array)
URL Encoding processing
rawurlencode(); (replace all non-alphanumeric characters into % followed by two hexadecimal digits)
urlencode(); (replace all non-alphanumeric characters into % followed by Two-digit hexadecimal, spaces become + signs)
rawurldecode(); (parse and restore %-encoded URLs)
urldecode(); (parse and restore %##-encoded URLs)
Construct query strings, etc.
http_build_query(); (Generate the request string after url-encoded)
PHP Learning String Courseware_PHP Tutorialphp string.rar

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319097.htmlTechArticleSyntax string string can be defined in three ways '' (single quote) "" (double quote) (delimited symbol) The escape sequence can be interpreted as a double quote using \. The biggest feature is that the variable name will be replaced by the variable value...
Statement:
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