SoFunction
Updated on 2025-03-10

PHP strings Small knowledge

Pay attention to the use of escape characters\\\,\",\$
Note that using ength or hexadecimal characters are used to represent \xf6
echo "H\xf6me";//You need to check whether this type of text encoding is supported
--------------------------------------------------------------------------------------------------------------------------------
H-cap e
---------------------------------------------------------------------
1. Create formatted output using printf() and sprintf()
printf() outputs 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 include d-integer, s-string, f-floating point, 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("%.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");
--------------------------------------------------------------------------------------------------------------------------------
3.14 3.143.1415900000abcdefghi 3.14,3.141590, 3.142\m1111011 123 123.000000 test
---------------------------------------------------------------------
2. String fill
string str_pad(string input original string, total length after adding int length[, string padding character to be filled [, int pad_type] padding type])
The fill type is added to the left STR_PAD_LEFT, which is added to the right by default, and the fill is added to the both ends STR_PAD_BOTH
$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 "
";
--------------------------------------------------------------------------------------------------------------------------------


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)//Capitals
string ucwords(string subject)//Capital capitalization of each word
string ltrim(string subject)//Remove the left blank
string rtrim(string subject)//Remove the right blank
string trim(string subject) removes the left and right blank spaces, including null, tab, newline, carriage return and spaces
string n12br(string source)//Convert the newline represented by \n to
mark
3. String comparison
integer strcmp(sting str1, string str2) //str1 is greater than str2 returns -1 str1 is less than str2 returns 1 str1 and str2 are equal to 0
integer strmcmp(sting str1, string str2, integer length) //The third parameter limits the comparison of length characters
print strcmp("aardvark","aardwolf");
print strncmp("aardvark","aardwolf",4);
--------------------------------------------------------------------------------------------------------------------------------
-10

---------------------------------------------------------------------
strcasecmp() and strncasecmp() are case-insensitive comparison functions
4. Find and extract substrings
string substr(sting source,integer start[,integer length])//From start with start, length characters
start and length can use negative values
$var = "abcdefgh";
print substr($var,2);//Count from 0
print substr($var,2,3);
print substr($var,-1);//Start from the end of the string
print substr($var,-5,2);
print substr($var,-5,-2);
--------------------------------------------------------------------------------------------------------------------------------
cdefgh
cde
h
de
def

---------------------------------------------------------------------
integer strpos(string haystack,string needle[,integer offset])//Find the location of the substring and return the first occurrence.
integer strrpos(string haystack,string needle)//Search only for a single character (only the first one of multiple characters) and return the index that appears last.
There are also common functions that extract the parts found from strings
string strstr(string haystack, string needle)//Case-insensitive
string string (string haystack, string needle)// case sensitive
string strrchr(string haystack,sting needle)
************ array exploit(string separator,string subject[,integer limit])//Returns an array of strings
array implode(string glue,array pieces)//Return a string
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$guest = "this is a string";
$guestArray = explode(" ",$guest);
var_dump($guestArray);
sort($guestArray);
echo implode(",",$guestArray);
////////////////////////////////////////////////////////////////////////
--------------------------------------------------------------------------------------------------------------------------------
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])