Summary of PHP string basics

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 indicates xf6echoHxf6me. do you need to check whether such text encoding is supported? Phpecho --------------------- output result ------------------------------------------; echoHxf6me; string required
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

Echo '--------------------- output result ----------------------------------------';

Echo "H \ xf6me \"; // check whether such text encoding is supported

Echo '---------------------------------------------------------------------';

?>

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 ");


Echo '--------------------- output result ----------------------------------------';

Printf ("%. 2f \", 3.14159 );

Printf (\ "% 10.2f \", 3.14159 );

Printf (\ "%. 10f \", 3.14159 );

Printf (\ "%. 9s \", abcdefghijklmn );

Printf (\ "% 5.2f, % f, % 7.3f \ m \", 3.14159, 3.14159, 3.14159 );

Printf (\ "% B % d % f % s \ n \", 123,123,123, \ "test \");

Echo '---------------------------------------------------------------------';

?>

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"

";

Echo '--------------------- output result ----------------------------------------';

$ 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 \"
\";

echo '---------------------------------------------------------------------';

?>

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) // converts a linefeed represented by \ n to a <BR/> flag
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 );

Echo '--------------------- output result ----------------------------------------';

Print strcmp ("mongodvark \", \ "mongodwolf \");

Print strncmp (\ "mongodvark \", \ "mongodwolf \", 4 );

Echo '---------------------------------------------------------------------';

?>

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 );

Echo '--------------------- output result ----------------------------------------';

$ Var = "abcdefgh \";

Print substr ($ var, 2 ).\"\";

Print substr ($ var, 2, 3 ).\"\";

Print substr ($ var,-1 ).\"\";

Print substr ($ var,-5, 2 ).\"\";

Print substr ($ var,-5,-2 ).\"\";

Echo '---------------------------------------------------------------------';

?>

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 );
//////////////////////////////////////// ////////////////////////////////


Echo '--------------------- output result ----------------------------------------';

$ Guest = "this is a string \";

$ GuestArray = explode (\ "\", $ guest );

Var_dump ($ guestArray );

Sort ($ guestArray );

Echo implode (\ ", \", $ guestArray );

Echo '---------------------------------------------------------------------';

?>

5. replace characters and substrings
String substr_replace (string source, string replace, int start [, int length])

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.