Summary of basic knowledge of PHP strings _php

Source: Internet
Author: User
Keywords Summary Basics String about String echo
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

echo '---------------------输出结果----------------------------------------';

echo "H\xf6me\";//需要察看是否支持此类文字编码

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

?>

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


echo '---------------------输出结果----------------------------------------';

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

";

echo '---------------------输出结果----------------------------------------';

$index = array("one\"=>1,\"two\"=>155,\"three\"=>1679);

echo \"

\";

echo str_pad(\"这是标题\",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)//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 <br/> tag
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);

echo '---------------------输出结果----------------------------------------';

print strcmp("aardvark\",\"aardwolf\");

print strncmp(\"aardvark\",\"aardwolf\",4);

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

?>

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

echo '---------------------输出结果----------------------------------------';

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


echo '---------------------输出结果----------------------------------------';

$guest = "this is a string\";

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

var_dump($guestArray);

sort($guestArray);

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

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

?>

5. Replacing 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.