One, String functions
1. Format string, using printf ()
Output to screen: printf ()
Output to Variable: sprintf ()
Tip: Two functions use the same method.
1.1 Data type conversions
printf ("This is my Number:%o",//this);
The conversion type starts with a% followed by the letter-represented type.
Note: You can have more than one% in a format control string, but the number of the second argument must be the same as the number of%.
type specifier
Specify a character |
Description |
D |
Display parameters in decimal numbers |
B |
Displays an integer in binary |
C |
Displays an integer in equivalent ASCII |
F |
Displays an integer as a floating-point number |
O |
Displays an integer in 8 |
S |
Display parameters as strings |
X |
Displays an integer in a lowercase hexadecimal |
X |
Displays an integer in an uppercase hexadecimal number |
1.2 Fill characters (specified length, not enough length to fill with specific characters)
printf ("%04d", 36);//output less than 4 bits, will be preceded by 0
//prints 0036
printf ("% 4d", 36);//output less than 4 digits, preceded by a space
//prints 36
printf ("% ' x4d", 36);/In addition to 0 and null, other padding characters must precede the single quotation mark
//prints "xx36"
//Hint: The browser will not display multiple spaces, you can add <pre> to the outside of the output tag to force the display of spaces and new lines.
echo "<pre>the spaces would be visible</pre>";
1.3 Specifying string output length
echo "<pre>\n";
printf ("%20s\n", "books"); The default right alignment, less than 20 digits, front fill space.
printf ("%20s\n", "CDs");
Left-aligned: Adds a minus sign to the front of the length
("%-20s\n", "left aligned");
echo "</pre>";
1.4 specified Precision (rounded)
printf ("%.2f", 5.3333);
Prints "5.33"
echo "\ n";
printf ("%.2f", 5.3353);
Prints "5.34"
1.5 Specify the order in which the parameters are displayed
$dates =array (Array (' Mon ' =>12, ' mday ' =>25, ' year ' =>2011),
array (' Mon ' =>1, ' mday ' =>23, ' year ') = >2012)
);
$format =include ("local_format.php");//If you want to change the display format, simply change the file
foreach ($dates as $date) {
printf ("$format", $ date[' mon ', $date [' Mday '], $date [' Year ']);
}
local_format.php File Contents
Return "%02d/%02d/%d<br/>",//mm/dd/yyyy return
"%2\$02d/%1\$02d/%3\ $d <br/>";//dd/mm/yyyy
/ /2\$: Second parameter placeholder
1.6 Store the formatted results to a variable
Preserves 2 decimal places and stores the results in $cash variables
$cash =sprintf ("%.2f", 21.334454);
echo "You have \$ $cash to spend."
Prints "You have $21.33 to spend."
A 2.1 string is an array of characters, and we can access individual characters just like the elements of an array.
$test = ' Phpcoder ';
echo $test [0];//prints "P"
echo $test [4];//prints "O"
2.2 strlen () Gets the length of the string. One Chinese character takes up 3 lengths
2.3 strstr ( source string, lookup substring) , gets all the strings from the beginning of the child string to the end, and returns False if it is not found
Distinguishes uppercase small
echo strstr ("PAB7", "AB") when compared,//prints "AB7"
//case-insensitive
echo stristr ("PAB7", "AB");//prints "AB7 "
Whether to include substring
$membership = "pAB7";
if (Strstr ($membership, "AB")) {
echo "<p>your membership expires soon!</p>";
}
2.4 Strpos (The source string, the lookup substring), gets the index of the substring
Gets the index of the substring, returns false
echo Strpos ($membership, "XY");//prints 4
2.5 substr (string, start index, length) intercept substring
$test = "Phpcoder";
Echo substr ($test, 3). " <br/> "//prints" coder "
Echo substr ($test, 3,2)." <br/>//prints "CO"
//Note: If the second argument is a negative number, the index is computed from the end, that is, the character is started at the end
$test = "pierre@wanadoo.fr";
if ($test =substr ($test, -3) = = ". Fr") {
echo ". Fr";
}
2.5 strtok (source string, delimited string), splitting string
$test = "Http://www.google.com/search";
$test. = "Hl=en&ie=utf-8&q=php+development+books&btng=google+search";
$delims = "?&";//delimiter can contain multiple arbitrary characters
$word =strtok ($test, $delims);//The first call, and return the first found decomposition while
is_string ($word) ) {//Why Test return type. Because there are multiple delimiters,
//If the two delimiters in the string together, when the first delimiter is encountered, it causes an empty string to be returned.
if ($word) {
echo $word. " <br/> ";
}
$word =strtok ($delims);//The second call only needs to pass the delimiter string
}
2.6 trim (), LTrim (), RTrim (), removing string spaces
2.7 strip_tags (source string, tag that needs to be preserved), delete the tag in the string
$string = "<p>\" I <em>simple</em> would not have it,\ "<br/>said Mr dean.</p><p>< Strong>the end.</strong></p> ";
Echo strip_tags ($string, "<br/><p>");//Only Keep "<br/><p>" mark, all other tags delete
echo strip_tags ($ string);//delete all HTML tags.
2.8 Substr_replace (source string, new string inserted, delete start index, deleted character length), replace character with new character at specified position
$membership = "mz11xyz";
Echo Substr_replace ($membership, "//prints", 2,2); "MZ89XYZ" replaces 2 characters
echo "\ n";
Echo Substr_replace ($membership, "//prints", 2,1); "MZ891XYZ" replaces only one character in the source string,
echo "\ n";
Echo Substr_replace ($membership, "2"),//prints "mz89" without length parameter, all characters after deletion position
2.9 str_replace ($search, $replace, $source) Replacement string
Note: The parameter can be an array or a string.
3.0 Case Conversion
Strtolower () All lowercase
Strtoupper () All Caps
Ucwords () capitalize the first letter of each word
Ucfirst () Capitalize first letter of string
3.1 characters Swop line
Converts a newline character in a string to a <br/> tag
$str = "one line\n";
$str = "another line\n";
echo nl2br ($STR);
Wrap text manually
$string = "As usual you can find me at http://www.witteringonaboutit.com/";
$string. = "chat/eating_green_cheese/forum.php." Hope to the there! ";
echo WordWrap ($string, "<br/>\n");
echo WordWrap ($string, "<br/>\n", 1)//force 24 characters to wrap
//Note: A third argument must be taken, otherwise the line will not wrap in the browser.
3.2 Explode () splits a string into an array
$start _date= "2012-02-19";
$date _arrary=explode ("-", $start _date);
Echo ($date _arrary[0]);//prints "2012"
Two, date and time functions
2.1 time () gets the timestamp of the current date
2.2 getdate (timestamp) returns an associative array of date information. The date with the current timestamp without the parameter.
Note: Set the default time zone, and if not set, the error is obtained.
modifying php.ini files
Date.timezone=prc
Restart Apache after modifying php.ini
or add code before the code is received:
Date_default_timezone_set (' PRC ');
2.3 Date (format string, timestamp) format timestamp
2.4 mktime (time, minutes, seconds, months, days, years) Create timestamp
2.5 checkdate () detection date. The Unix timestamp starts on January 1, 1970, and the previous date is invalid (negative) time stamp