Five tips for string processing in PHP programming

Source: Internet
Author: User

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

Related Article

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.