Based on PHP common function Summary (array, string, time, file operation) _php tips

Source: Internet
Author: User
Tags create directory current time getdate int size local time locale locale setting string format

Array: "Focus 1"
implode (delimited, arr) concatenate the array value data by the specified character
For example:
$arr =array (' 1 ', ' 2 ', ' 3 ', ' 4 ');
$str =implode ('-', $arr);
Explode ([delimited],arr) splits a string by the specified rule, returning a value of number group name join
Array_merge () merging one or more arrays
Array_combine (array keys, array Values) creates an array with the value of one array as its key name and the value of the other array as its value
for example:
$a = array (' green ', ' red ', ' yellow ');
$b = Array (' avocado ', ' apple ', ' banana ');
$c = Array_combine ($a, $b);
Print_r ($c);
/* Outputs:
Array
(
[green] => avocado
[red] => Apple
[yellow] => banana
)
*/
Array_push (ARR,STR) pushes one or more cells into the end of the array (in stacks)
Array_unique (arr) moves the duplicate values in the array
Array_search () searches for the given value in the arrays, and returns the corresponding key name if successful
Array_values () returns all values in the array
Array_keys () returns the number of cells in the array or the number of properties in the object sizeof ()
Is_array () in the array of all the key names
Count (arr) Detects whether a variable is an array of
sort (arr) Arrays
Array_flip (arr) commutative array of keys and values
Note that the value in trans needs to be able to be a valid key name, such as an integer or string. A warning is issued if the type of the value is not correct, and the problematic key/value pair will not be reversed.
Key (ARR) returns the key name of the current element in the array
(ARR) returns the value that the current pointer points to
next returns the value pointed to by the next pointer
for example

Copy Code code as follows:

$array = Array (
' Fruit1 ' => ' Apple ',
' Fruit2 ' => ' orange ',
' Fruit3 ' => ' grape ',
' Fruit4 ' => ' Apple ',
' Fruit5 ' => ' apple '
);
This cycle echoes the all associative array
Key where value equals "apple"
while ($fruit _name = current ($array)) {
if ($fruit _name = = ' Apple ') {
echo Key ($array). ' <br> ';
}
Next ($array);
}

The internal pointer to the reset (arr) array points to the first cell
Array_chunk (array input, int size [, bool Preserve_keys]) splits an array into multiple
Splits an array into multiple arrays, where the number of units per array is determined by size. The number of cells in the last array may be few. The resulting array is a cell in a multidimensional array whose index starts at zero.
Setting the optional parameter Preserve_keys to TRUE allows PHP to retain the original key name in the input array. If you specify FALSE, then each result array will be indexed with a new zero-based number. The default value is FALSE.

string "2"
trim (str) eliminates spaces on either side of the string
RTrim ()
Addslashes The length of the string before the specified predefined characters plus \
strlen (str)
substr (str,start,length) intercepts the characters of the specified length in the specified string
strstr (str,search) function to get a substring of the first occurrence of the specified string in another string to the end of the latter. STRCHR the same
Strpos (str,search) lookup string where a character first appears
Str_replace (SEARCH,REPLACE,STR) string replacement
Ucfirst (str) Capitalize the first character of the string
Strtolower to lowercase
ucwords (str) to compare the first letter of each word in the string to the
strcmp (STR1,STR2) function for comparing
Substr_count () function retrieves the number of occurrences of a substring
a regular correlation string function:
Preg_match (pattern,subject,matches) searches the subject string for what matches the regular expression given by pattern, The result of the match is stored in matches, Matches[0] matches the content, matches[1 the first mode cell matches[1 the second mode unit, and so on.
Preg_match_all (pattern,subject,matches) global match, with the rest of the Preg_match functions similar
preg_replace (PATTERN,REPLACEMENT,STR) Performs search and replace of regular expressions, three [String,string][array,string][array,array]
Preg_split (PATTERN,STR) split strings with regular expressions
Preg_grep ( Pattern,array) matches the value of the array with a regular expression, returning a new array information

Time "Key 3"
PHP Date-time function date ()
1, year-month-day
echo Date (' y-m-j ');
2007-02-6
echo Date (' y-n-j ');
07-2-6
Capital Y represents a four-digit year, while lowercase y represents a two-digit number for the year;
Lowercase m represents the number of months (with leading), while lowercase n indicates the number of months without leading.
echo Date (' y-m-j ');
2007-feb-6
echo Date (' y-m-d ');
2007-02-06
Uppercase M represents the 3 abbreviations for the month, while lowercase m represents the number of months (with a leading 0);
There is no uppercase J, only lowercase J indicates the date of the month, no leading o, and lowercase D if you want the month band leading.
echo Date (' y-m-j ');
2007-feb-6
echo Date (' Y-f-js ');
2007-february-6th
Capital M represents the 3 abbreviations of the month, while capital F indicates the full English of the month. (No lowercase f)
Uppercase S represents the suffix of a date, such as "St", "nd", "rd", and "th", depending on the date number.

Summary:
Indicates that the year can be in uppercase Y and lowercase y;
Indicates that the month can be in uppercase F, uppercase M, lowercase m, and lowercase n (two ways of representing characters and numbers, respectively);
Indicates that the day can be a suffix of a date with lowercase d and lowercase j, uppercase S.

2, when: minutes: seconds
By default, PHP interprets the time as "Greenwich Mean Time", which is 8 hours away from our local time.
echo Date (' g:i:s a ');
5:56:57 am
echo Date (' H:i:s A ');
05:56:57 AM
Lowercase g represents a 12-hour system with no leading 0, while lowercase h indicates a 12-hour system with a leading 0.
When using a 12-hour system, you need to indicate that in the afternoon, lowercase a is the lowercase "am" and "PM", and capital a denotes "AM" and "PM" uppercase.
echo Date (' g:i:s ');
14:02:26
Capital G represents 24 hours of hours, but not leading; use uppercase H to indicate a 24 hour system with a leading number of hours
Summary:
The letter G indicates that the hour is not leading, and the letter H denotes the hour with leading;
Lowercase g, h means 12-hour system, capital G, H is 24-hour system.

3, Leap year, week, day
echo Date (' L ');
Whether this year leap years: 0
echo Date (' L ');
Today is: Tuesday
echo Date (' D ');
Today is: Tue
Capital L indicates whether a leap year is a Boolean value that returns 1 for true or 0;
Lowercase L means the day is the week of the English full write (Tuesday);
Instead, use uppercase D to denote the 3-character abbreviation for the Day of the Week (Tue).
echo Date (' W ');
Today's Week: 2
echo Date (' W ');
This week is the No. 06 week of the year.
The lowercase w represents the day of the week, the number form represents
Capital W indicates the number of weeks in a year
echo Date (' t ');
This month is 28 days
echo Date (' Z ');
Today is the 36th day of the year.
Lowercase T represents the current month and how many days
Lowercase Z means today is the first day of the year

4, other
echo Date (' T ');
Utc
Capital T indicates the time zone setting of the server
echo Date (' I ');
0
Capital I indicates whether the current is daylight saving time, returns 1 for true, or 0
echo Date (' U ');
1170769424
The capital U represents the total number of seconds from January 1, 1970 to the present, which is the Unix time stamp for the Unix time era.
echo Date (' C ');
2007-02-06t14:24:43+00:00
Lowercase C represents the ISO8601 date, the date format is YYYY-MM-DD, the date and time is separated by the letter T, the time format is HH:MM:SS, and the time zone uses the deviation of Greenwich Mean Time (GMT).
echo Date (' R ');
Tue, Feb 2007 14:25:52 +0000
Lowercase R represents the RFC822 date.
The small date () function shows PHP's powerful features and charming charm, and then compare the ASP, hehe.
Checkdate ($month, $date, $year)
If the value that is applied constitutes a valid date, the function returns True. For example, for an error date of February 31, 2005, this function returns to False.
You can use this function to check the date and make the date effective before the date is calculated or saved in the database.

Copy Code code as follows:

<?php
Returns false
echo checkdate (2,30,2005)? "Valid": "Invalid";
Returns True
echo checkdate (4,6,2010)? "Valid": "Invalid";
?>

getdate ($ts)
In the absence of an argument, the function returns the current date and time in a associative array. Each element in the array represents a specific component in the date/time value. You can submit an optional time label argument to a function to get the date/time value that corresponds to the time label.
This function is used to obtain a series of discrete, easily separated date/time values.
Copy Code code as follows:

<?php
Get date as associative array
$arr = getdate ();
echo "Date is". $arr [' Mday ']. " " . $arr [' Weekday ']. " " . $arr [' Year '];
echo "Time is". $arr [' hours ']. ":" . $arr [' minutes '];
?>

mktime ($hour, $minute, $second, $month, $day, $year)
The function is the opposite of getdate (): It generates a UNIX time label from a series of date and time values (the number of seconds from GMT January 1, 1970 to the current elapsed). When the argument is not used, it generates the UNIX time label for the current time.
Use this function to get the UNIX time label for the instant time. This time tag is commonly used in many database and program languages.
Copy Code code as follows:

<?php
Returns timestamp for 13:15:23 7-jun-2006
echo Mktime (13,15,23,6,7,2006);
?>

Date ($format, $ts)
This function formats the UNIX time label as a date string that can be read artificially. It is the most powerful function in the PHP date/Time API, and can be used in a series of correction values to convert the integer time label to the desired string format.
This function is applied to display the formatting time or date.
Copy Code code as follows:

<?php
Format Current Date
Returns "13-sep-2005 01:16 PM"
echo Date ("D-m-y h:i A", Mktime ());
?>

Strtotime ($STR)
This function converts an English date/time string that can be artificially read into a UNIX time label.
Apply this function to convert an nonstandard date/time string into a standard, compatible UNIX time label.
Copy Code code as follows:

<?php
Returns 13-sep-05
echo Date ("D-m-y", Strtotime ("Today"));
Returns 14-sep-05
echo Date ("D-m-y", Strtotime ("Tomorrow"));
Returns 16-sep-05
echo Date ("D-m-y", Strtotime ("Today +3"));
?>

strftime ($format, $ts)
As defined by the previous setlocale () function, this function formats the UNIX time label as a date string that applies to the current environment.
Apply this function to establish a date string that is compatible with the current environment.
Copy Code code as follows:

<?php
Set Locale to France (on Windows)
SetLocale (Lc_time, "Fra_fra");
Format month/day Names
As per locale setting
Returns "Septembre" and "mardi"
Echo strftime ("Month:%B");
echo strftime ("Day:%A");
?>

Microtime ()
As defined by the previous setlocale () function, this function formats the UNIX time label as a date string that applies to the current environment.
Apply this function to establish a date string that is compatible with the current environment.
Copy Code code as follows:

<?php
Get starting value
$start = Microtime ();
Run some code
For ($x =0 $x <1000; $x + +) {
$null = $x * $x;
}
Get ending value
$end = Microtime ();
Calculate time taken for code execution
echo "Elapsed time:". ($end-$start). " SEC ";
?>

gmmktime ($hour, $minute, $second, $month, $day, $year)
This function generates a UNIX time label from a series of date and time values expressed in GMT time. When you do not use a variable, it generates a UNIX time label for the current GMT instant time.
Use this function to obtain the UNIX time label for GMT Instant time.
Copy Code code as follows:

<?php
Returns timestamp for 12:25:23 9-jul-2006
echo Gmmktime (12,25,23,7,9,2006);
?>

gmdate ($format, $ts)
This function formats the UNIX time label as a date string that can be read artificially. This date string is represented as GMT (not local time).
This function is applied when the time label is represented by GMT.
Copy Code code as follows:

<?php
Format current date into GMT
Returns "13-sep-2005 08:32 AM"
Echo gmdate ("D-m-y h:i A", Mktime ());
?>

Date_default_timezone_set ($tz), Date_default_timezone_get ()
This function then all the date/time function calls set and restores the default time zone.
Note: This function is only valid in PHP 5.1+.
This function is a convenient shortcut for setting a time zone for future time operations.
Copy Code code as follows:

<?php
Set TimeZone to UTC
Date_default_timezone_set (' UTC ');
?>

Copy Code code as follows:

<?php
Today
$today = Date ("y-m-d");
Yesterday
$yesterday = Date ("y-m-d", Strtotime (Date ("y-m-d"))-86400;
Last week
$lastweek _start = Date ("y-m-d h:i:s", mktime (0, 0, 0,date ("M"), Date ("D")-date ("W") +1-7,date ("Y")));
$lastweek _end = Date ("y-m-d h:i:s", Mktime (23,59,59,date ("M"), Date ("D")-date ("W") +7-7,date ("Y"));
Week
$thisweek _start = Date ("y-m-d h:i:s", mktime (0, 0, 0,date ("M"), Date ("D")-date ("W") +1,date ("Y")));
$thisweek _end = Date ("y-m-d h:i:s", Mktime (23,59,59,date ("M"), Date ("D")-date ("W") +7,date ("Y"));
Last month
$lastmonth _start = Date ("y-m-d h:i:s", mktime (0, 0, 0,date ("M") -1,1,date ("Y")));
$lastmonth _end = Date ("y-m-d h:i:s", Mktime (23,59,59,date ("M"), 0,date ("Y")));
Month
$thismonth _start = Date ("y-m-d h:i:s", mktime (0, 0, 0,date ("M"), 1,date ("Y")));
$thismonth _end = Date ("y-m-d h:i:s", Mktime (23,59,59,date ("M"), Date ("T"), Date ("Y"));
Not last month of this quarter
$getMonthDays = Date ("T", mktime (0, 0, 0,date (' n ') + (date (' n ')-1)%3,1,date ("Y"));
This quarter/
$thisquarter _start = Date (' y-m-d h:i:s ', mktime (0, 0, 0,date (' n ')-(date (' n ')-1)%3,1,date (' Y '));
$thisquarter _end = Date (' y-m-d h:i:s ', Mktime (23,59,59,date (' n ') + (date (' n ')-1)%3, $getMonthDays, date (' Y '));

The

file Operation key 4
file_exists (filename) file or directory exists
FileSize (filename) to get the file size
PathInfo ( FileName) returns an associative array of directory names, base names, and extensions
$path _parts = PathInfo ("/www/htdocs/index.html");
echo $path _parts["DirName"]. "\ n"; The
Echo $path _parts["basename"]. "\ n"; The
Echo $path _parts["extension"]. "\ n";
/www/htdocsindex.htmlhtml
mkdir (dirname) Create directory
RmDir (dirname) Delete empty directory
fopen (filename, mode) Open file
Fclose (FP) Closes the file
fwrite (fp,str,length) write file
File_put_contents (filename,content) saves the content as a file
file_get_contents ( FileName) read from the file
Fread (fp,length) Read the file
Fgets () reads a row from the file pointer
fgetc () reads the character from the file pointer
file () reads the entire file into an array, Each cell in the array is a corresponding line in the file
ReadFile () reads a file and writes to the output buffer
Copy (filename1,filename2) copy file
unlink (filename) to delete the file
Rename (filename1,filename2) renames the file or directory
$text = iconv (' GBK ', ' utf-8 ', ' Chi-wai ');///converts GBK encoding to utf-8

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.