Getting Started: Dynamic Web page PHP programming string 5 Tips

Source: Internet
Author: User
Tags comparison explode integer php programming printf sprintf strcmp

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 types have D-integers, S-strings, F-floating-point numbers, 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.14159,3.14159,3.14159);
printf ("%b%d%f%s \ n", 123,123,123, "test");
---------------------Output----------------------------------------
3.14 3.143.1415900000abcdefghi 3.14,3.141590, 3.142\m1111011 123 123.000000 Test
---------------------------------------------------------------------

2. String padding

String Str_pad (String input raw string, total length added after int 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", "M", "", Str_pad_both). " \ n ";
foreach ($index as $inkey => $inval)
Echo Str_pad ($inkey, 30, "."). Str_pad ($inval, ".", 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) to left and right blank, including null, tab, newline, carriage return, and space
String n12br (string source)//converts the newline character \ n represented to the <BR/> Tag

3. String comparison

Integer strcmp (Sting str1,string str2)//str1 greater than str2 return-1 str1 less than str2 return 1 str1 and str2 equal return 0
Integer strmcmp (Sting str1,string Str2,integer 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 source,integer 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, 2, 3);
Print substr ($var,-1);//start at the end of the string
Print substr ($var, -5,2);
Print substr ($var, -5,-2);
---------------------Output----------------------------------------
Cdefgh
Cde
H
De
Def

---------------------------------------------------------------------
Integer Strpos (string haystack,string needle[,integer offset])//finds the position of the substring, and returns the first occurrence.
The integer strrpos (string haystack,string 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 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 an arrays of strings
Array implode (string glue,array 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"} a,is,str Ing,this
---------------------------------------------------------------------

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.