- $STR = "Hello World". It ' s a beautiful day. ";
- Print_r (Explode ("", $str));
- ?>
Copy Code
2, String Connection example:
- Defining strings
- $str 1 = "Hello world!";
- $str 2 = "PHP!";
- Connect the above two strings separated by a space
- $str 3 = $str 1. " " . $STR 2;
- Output the concatenated string
- echo $str 3;
Copy Code
3, the string interception substr (), mb_substr example:
- $str = "login.com";
- Echo substr ($STR, 2); Gin.com
- Echo substr ($str, 2,3); Gin
- Echo substr ($str,-2); Om negative number starts at the end of the fetch
- ?>
Copy Code
But when you intercept the Chinese string, it is easy to garbled, because a Chinese character is two bytes, and an English letter is a byte.
Workaround: 2,mb_substr (), use the same method and substr, but to open the php.ini inside the extension=php_mbstring.dll extension.
Example:
- echo mb_substr ("int Hello", 1,3, "UTF-8"); NT You
- ?>
Copy Code
3. String lookup Strpos () definition and usage the Strpos () function returns the position of the first occurrence of a string in another string. If the string is not found, false is returned.
Grammar Strpos (String,find,start)
Parameter description string required to specify the string to be searched find must specify the character to look for. Start is optional. Specify where to start the search
Example:
- $string = ' #11 #22#33#44#55#66#77 ';
- $find = ' #55 ';
- if (Strpos ($string, $find) ===false) {
- echo ' not found ';
- }else{//bbs.it-home.org
- echo ' Find ';
- }
- Echo Strpos ($string, $find);
- ?>
Copy Code
4, String substitution str_replace Example:
- $replace _string = ' You are bad, really bad ';
- $replace _string_b = ' very bad ';
- $replace _string_ = Str_replace ($replace _string_b, ' * ', $replace _string, $i);
- echo $replace _string_;
- ?>
Copy Code
Specify location Replacement Substr_replace
- Echo substr_replace ("Hello World", "PHP12", 6); Hello PHP12
- ?>
Copy Code
5, string-to-case function converts a string to lowercase strtolower (): The function converts all characters passed in the string argument to lowercase and returns the string in a small form. Example:
- $str = "I love You";
- $str = Strtolower ($STR);
- Echo $str;
- ?>
Copy Code
Output result: I love you
Convert character to uppercase Strtoupper (): This function, in contrast to the Strtolower function, converts the character of the passed character argument all to uppercase and returns the string in uppercase. Use the same as Strtolowe ().
Converts the first character of a string to uppercase Usfilst (): The function is to change the first character of the string to uppercase, which returns a string with the first character capitalized. Use the same as Strtolowe ().
Converts the first character of each word in a string to uppercase Ucwords (): The function capitalizes the first character of each word in the passed-in string. such as "Hello World", after the function is processed, will return "Hello Word." Use the same as Strtolowe ().
>>> See more PHP Getting Started tutorials <<<
|