5 tips for string handling in PHP programming _php tutorial

Source: Internet
Author: User
Tags php programming strcmp
String
Note the difference between single and double quotation marks
Note the escape character \ 's use \\,\ ", \$
Note Using 8-or 16-character notation \xf6
echo "H\xf6me";//need to see if this type of text encoding is supported
---------------------The result of the output----------------------------------------
H 鰉 E
---------------------------------------------------------------------

1. Use printf () and sprintf () to create formatted output

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 start with%
Data types are D-integers, S-strings, F-floating-point numbers, B-binary
.2 is an optional width indicator, the right side of the decimal point output uses 0 padding
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");
---------------------The result of the 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 primitive string, int length added after total lengths [, string padding characters to be populated [, int pad_type] Fill type])
The fill type has been added to the left Str_pad_left, the default added to the right, padding at both ends Str_pad_both
$index = Array ("One" =>1, "=>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, ".", Str_pad_left). " \ n ";
echo "
";
---------------------The result of the output----------------------------------------

This is the title
One .... ..... ..... ....... ..................... 1
A. ..... ..... ....... .................... 155
Three ........ ....................... 1679

---------------------------------------------------------------------
String Strtolower (String subject)//convert to lowercase
String Strtoupper (String subject)//Convert to uppercase
String Ucfirst (String subject)//Capitalize first letter
String Ucwords (String subject)//capitalize each word first letter
String LTrim (String subject)//left blank
String RTrim (String subject)//GO right blank
String trim (string subject) go left and right blank, blank includes null, tab, newline, carriage return and space
String n12br (string source)//Converts the newline character represented by \ n to
Mark

3. String comparisons

Integer strcmp (Sting str1,string str2)//str1 greater than str2 return-1 str1 less than STR2 returns 1 str1 and str2 equals return 0
Integer strmcmp (Sting str1,string Str2,integer length)//Third parameter limit comparison of length characters
Print strcmp ("Aardvark", "Aardwolf");
Print strncmp ("Aardvark", "Aardwolf", 4);
---------------------The result of the output----------------------------------------
-10
---------------------------------------------------------------------
STRCASECMP () and strncasecmp () are case-insensitive comparison functions

4. Finding and extracting substrings

String substr (Sting source,integer Start[,integer length])//length of 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);
---------------------The result of the output----------------------------------------
Cdefgh
Cde
H
De
Def
---------------------------------------------------------------------
Integer Strpos (string haystack,string needle[,integer offset])//finds the position of the substring, returning the first occurrence.
The integer strrpos (string haystack,string needle)//searches only a single character (more than one character takes the first one), returning 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 array 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);
////////////////////////////////////////////////////////////////////////
---------------------The result of the 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. Replacing characters and substrings

String Substr_replace (String source,string replace,int start[,int length])

http://www.bkjia.com/PHPjc/318586.html www.bkjia.com true http://www.bkjia.com/PHPjc/318586.html techarticle Note the difference between single and double quotes in the string note the escape character \ \\,\ ", \$ note that the \xf6 echo" H\xf6me "is denoted by using 8-or 16-character characters;//need to see if such text is supported ...

  • 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.