Common sense of "String" in PHP programming

Source: Internet
Author: User
Note the differences between single and double quotation marks for strings. Note the use of escape characters \, \, \ $. Note that the use of octal or hexadecimal characters represents \ xf6echoH \ xf6me; // check whether such text encoding is supported --------------------- output result ------------------------------------------ H e ---

String

Note the difference between single quotes and double quotes

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

---------------------------------------------------------------------

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.