PHP string tips _ PHP Tutorial

Source: Internet
Author: User
PHP string tips. Pay attention to the use of escape characters \, $ pay attention to use octal or hexadecimal characters to represent xf6echoHxf6me; check whether such text encoding is supported ------------------- output result --- note the use of escape characters \, \ ", \ $
Use octal or hexadecimal characters to represent \ xf6
Echo "H \ xf6me"; // check whether such text encoding is supported
--------------------- Output result ----------------------------------------
H e
---------------------------------------------------------------------
1. use printf () and sprintf () to create formatted output
Printf () is directly output to the output buffer.
Sprintf () output is returned as a string
For example, printf ("output content %. 2f \ n", $ PI ());
All conversion rules start with %
Data types include d-integer, s-string, f-floating point number, and B-binary.
. 2 is an optional width indicator, and the output to the right of the decimal point is filled with 0
Printf ("%. 2f", 3.14159 );
Printf ("% 10.2f", 3.14159 );
Printf ("%. 10f", 3.14159 );
Printf ("%. 9 s", abcdefghijklmn );
Printf ("% 5.2f, % f, % 7.3f \ m", 3.14159, 3.14159, 3.14159 );
Printf ("% B % d % f % s \ n", 123,123,123, "test ");
--------------------- Output result ----------------------------------------
3.14 3.143.1415900000abcdefghi 3.14, 3.141590, 3.142 \ m1111011 123 123.000000 test
---------------------------------------------------------------------
2. string filling
String str_pad (string input original string, total length after int length is added [, string padding character to be filled [, int pad_type] fill type])
The fill type is added to STR_PAD_LEFT on the left. the fill type is added to the right by default, and the fill type is added to STR_PAD_BOTH on both ends.
$ Index = array ("one" => 1, "two" => 155, "three" => 1679 );
Echo"
";
Echo str_pad ("This is the title", 50, "", STR_PAD_BOTH). "\ n ";
Foreach ($ index as $ inkey => $ inval)
Echo str_pad ($ inkey, 30, "."). str_pad ($ inval, 20, ".", STR_PAD_LEFT). "\ n ";
Echo"
";
--------------------- Output result ----------------------------------------


This is the title
One ....................................... ....... 1
Two ....................................... ..... 155
Three ....................................... .. 1679


---------------------------------------------------------------------
String strtolower (string subject) // Convert to lowercase
String strtoupper (string subject) // Convert to uppercase
String ucfirst (string subject) // uppercase letters
String ucwords (string subject) // uppercase letters of each word
String ltrim (string subject) // remove left blank
String rtrim (string subject) // remove the right blank
String trim (string subject) removes the left and right white spaces. the blank spaces include null, tabs, line breaks, carriage returns, and spaces.
String n12br (string source) // Convert the linefeed \ n
Mark
3. string comparison
Integer strcmp (sting str1, string str2) // str1 greater than str2 returns-1 str1 less than str2 returns 1 str1 and str2 equal return 0
Integer strmcmp (sting str1, string str2, integer length) // comparison of the third parameter that limits length characters
Print strcmp ("mongodvark", "mongodwolf ");
Print strncmp ("mongodvark", "mongodwolf", 4 );
--------------------- Output result ----------------------------------------
-10

---------------------------------------------------------------------
Strcasecmp () and strncasecmp () are case-insensitive comparison functions.
4. search for and extract substrings
String substr (sting source, integer start [, integer length]) // The length of a string starting from start.
Negative values can be used for start and length.
$ Var = "abcdefgh ";
Print substr ($ var, 2); // count from 0
Print substr ($ var, 2, 3 );
Print substr ($ var,-1); // starts from the end of the string
Print substr ($ var,-5, 2 );
Print substr ($ var,-5,-2 );
--------------------- Output result ----------------------------------------
Cdefgh
Cde
H
De
Def

---------------------------------------------------------------------
Integer strpos (string haystack, string needle [, integer offset]) // locate the substring and return the first occurrence.
Integer strrpos (string haystack, string needle) // only searches for a single character (multiple characters are the first) and returns the last index.
There are also common functions for extracting the part of a string.
String strstr (string haystack, string needle) // case insensitive
String stristr (string haystack, string needle) // case sensitive
String strrchr (string haystack, sting needle)
* ********** Array explode (string separator, string subject [, integer limit]) // returns a string array
Array implode (string glue, array pieces) // returns a string
/// // Code segment //////////// ////////////////////////////
$ Guest = "this is a string ";
$ GuestArray = explode ("", $ guest );
Var_dump ($ guestArray );
Sort ($ guestArray );
Echo implode (",", $ guestArray );
//////////////////////////////////////// ////////////////////////////////
--------------------- Output result ----------------------------------------
Array (4) {[0] => string (4) "this" [1] => string (2) "is" [2] => string (1) "a" [3] => string (6) "string"} a, is, string, this
---------------------------------------------------------------------
5. replace characters and substrings
String substr_replace (string source, string replace, int start [, int length])

", \ $ Note that the \ xf6 echo" H \ xf6me "is represented by octal or hexadecimal characters "; // check whether such text encoding is supported ------------------- output result ---...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.