: This article mainly introduces php strings (I). For more information about PHP tutorials, see. In C, strings are processed as byte arrays. In Java, strings are processed as objects. Php treats the string as the basic data type. The processing of strings usually involves the formatting of strings. String segmentation and connection, string comparison, and string search, matching, and replacement.
String Processing
"; Echo $ str {0}. $ str {1 }."
"; $ S =" "; for ($ I = 0; $ I
"; Echo strlen (" China ");
// Echo count ("abc"); define ("one", "two"); $ int = array ("one" => 100, "two" => 200); class Demo {var $} $ d = new Demo; echo "aaaaaaaaaaaa {$ d-> one} aaaaaaaaaaaaaa
"; // Echo" aaaaaaaaaaaaaa {$ int ["one"]} aaaaaaaaaaaaaa
";
"; // Multi-byte low efficiency usage same as single-byte echo substr ($ str1 );
String output
Common output string functions:
Echo () -- output string
Print () -- output a string
Die () -- output a message and exit the current script.
Printf () -- output formatted string
Sprintf () -- Write formatted strings into a variable
Differences between echo and print
1. echo is a command + function print is a function
2. echo has no return value. print has a return value.
3. echo can print multiple values separated by commas without parentheses; print is not allowed
No one uses print.
Die ()/exit () meaning: parameters in the brackets after the program is output and exited indicate the status 0-254
Both printf and sprintf are formatted strings:
String conversion format:
% Return percent sign
% B binary
% C characters based on ASCII values
% D signed decimal number
% E resumable counting (for example, 1.5e3)
% U unsigned decimal number
% F or % F floating point number
% O octal values
% S string
% X or % X hexadecimal number
It's just something in C language. for example, this knowledge point is over.
Common string formatting functions
Remove space and string filling completion functions
Function: ltrim ()
Syntax: string ltrim (string str [, string charlist]);
Return value: string
This function is used to delete leading spaces (whitespace) in a string ).
Function: rtrim () also has an alias: chop ()
Syntax: string rtrim (string str [, string charlist]);
Return value: string
This function is used to delete the suffix space (whitespace) in the string ).
Function: trim ()
Syntax: string trim (string str [, string charlist]);
Return value: string
Removes spaces at the beginning and end of a string. This function returns the string removed from the white space at the beginning and end of the string.
Function: fill the string as needed by str_pad.
Syntax: string str_pad (string input, int pad_length [, string pad_string [, int pad_type]) by default, it is filled with spaces.
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 case-sensitive conversion
Function: strtolower ()
Syntax: string strtolower (string str );
This function reduces all str strings and writes them into strings.
Function: strtoupper ()
Syntax: string strtoupper (string str );
This function converts all str strings to uppercase strings.
Function: ucfirst () was used a few days ago.
Change the first character of the string to uppercase.
Syntax: string ucfirst (string str );
This function returns the first letter of the string 'str' in uppercase.
Function: ucwords ()
Change the first letter of each character in the string to uppercase.
Syntax: string ucwords (string str );
This function returns the string 'str', where all the first letters of each character are converted to uppercase letters.
The above introduces the php string (I), including the content, and hope to be helpful to friends who are interested in the PHP Tutorial.