The little common sense of PHP "strings"

Source: Internet
Author: User
Tags comparison explode functions integer printf sprintf strcmp
Note the difference between single and double quotes in a string note the escape character \ 's use \\\ "\$ Note using a 8-in or 16-character representation \xf6

String
Note the difference between single and double quotes
Note the escape character \ 's use \\\ "\$
Note Using 8-or 16-character representations of \xf6

echo "H\xf6me";/need to see if this type of text encoding is supported

---------------------Output----------------------------------------

H 鰉 E

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

1. Create formatted output using printf () and sprintf ()

printf () output directly to the output buffer

The output of sprintf () is returned as a string

such as printf ("Output content%.2f\n" $PI ());

All conversion specifications begin with%

Data type has D-integer s-string F-floating-point number B-binary

.2 is an optional width metric, 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 ("%.9s" ABCDEFGHIJKLMN);

printf ("%5.2f%f%7.3f\m" 3.141593.141593.14159);

printf ("%b%d%f%s \ n" 123123123 "test");

---------------------Output----------------------------------------

3.14 3.143.1415900000abcdefghi 3.143.141590 3.142\m1111011 123 123.000000 Test

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

2. String padding

String Str_pad (String input raw string int length added total length [string padding the character to be populated [int pad_type] Fill type])

The fill type has added to the left Str_pad_left, the default adds to the right, padding at both ends Str_pad_both

$index = Array ("One" =>1 "two" =>155 "three" =>1679);

echo "

";

Echo Str_pad ("This is the title" "Str_pad_both"). " \ n ";

foreach ($index as $inkey => $inval)

Echo Str_pad ($inkey 30 "."). Str_pad ($inval 20 "." Str_pad_left). " \ n ";

echo "

";

---------------------Output----------------------------------------

That's the title.

One .... ..... ...... ...... ....... ...... ...... ....... ... 1

Two ..... ....... ...... ........ ...... ...... ..... ... 155

Three ..... ....... ....... ....... ...... ...... ... 1679

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

Convert string Strtolower (string subject)//to lowercase

String Strtoupper (String subject)//Convert to uppercase

String Ucfirst (String subject)//First letter uppercase

String Ucwords (String subject)//Capitalize first letter of each word

String LTrim (String subject)/Go left blank

String RTrim (String subject)/GO right blank

String trim (string subject) goes left and right, white space includes null tabs, newline characters, carriage returns, and spaces

String n12br (string source)//converts the newline character \ n represented to
Mark

3. String comparison

Integer strcmp (Sting str1string str2)//str1 greater than str2 return-1 str1 less than str2 return 1 str1 and str2 equal return 0

Integer strmcmp (Sting str1string Str2integer length)//third parameter limit length characters comparison

Print strcmp ("Aardvark" "Aardwolf");

Print strncmp ("Aardvark" "Aardwolf" 4);

---------------------Output----------------------------------------

-10

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

STRCASECMP () and strncasecmp () are case-insensitive comparison functions

4. Find and extract substrings

String substr (Sting sourceinteger Start[integer length])//length characters from start

Start and length can use negative values

$var = "ABCDEFGH";

Print substr ($var 2);//counting starting from 0

Print substr ($var 23);

Print substr ($var-1);//start at the end of the string

Print substr ($var-52);

Print substr ($var-5-2);

---------------------Output----------------------------------------

Cdefgh

Cde

H

De

Def

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

Integer Strpos (string haystackstring needle[integer offset])//finds the position of the substring, and returns the first occurrence.

The integer strrpos (string haystackstring needle)//searches only for a single character (multiple characters are only the first) and returns the last occurrence of the index.

There are also common functions for extracting parts found from strings.

String Strstr (String haystackstring needle)//case-insensitive

String Stristr (String haystackstring needle)//Case Sensitive

String STRRCHR (String haystacksting needle)

Array explode (string separatorstring subject[integer limit])//Returns an arrays of strings

Array implode (string gluearray pieces)//returns a string

Code Snippet////////////////////////////////////////

$guest = "This is a string";

$guestArray = Explode ("" $guest);

Var_dump ($guestArray);

Sort ($guestArray);

echo Implode ("" $guestArray);

////////////////////////////////////////////////////////////////////////

---------------------Output----------------------------------------

Array (4) {[0]=> string (4) "This" [1]=> string (2) "is" [2]=> string (1) "A" [3]=> string (6) "string"} Aisstrin Gthis

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

5. Replace characters and substrings

String Substr_replace (String sourcestring replaceint 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.