Summary of common functions based on PHP (array, string, time, file operations) _php Tutorial

Source: Internet
Author: User
Tags echo date locale setting
Array: "Focus 1"
Implode (delimited, arr) connects the array value data to the specified characters.
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 an 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) presses one or more cells into the end of the array (into the stack)
Array_unique (arr) to move duplicate values in the divisor group
Array_search () searches the array for the given value and returns the corresponding key name if successful
Array_values () returns all values in the array
Array_keys () returns all the key names in the array
COUNT (arr) calculates the number of cells in an array or the number of attributes in an object sizeof ()
Is_array (ARR) detects if a variable is an array
Sort (arr) sorting arrays
Array_flip (ARR) swaps the keys and values in an array
Note that the value in trans needs to be able to be a valid key name, such as an integer or string. If the value is of the wrong type, a warning will be issued and the problematic key/value pair will not be reversed.
Key (ARR) returns the key name of the current element in the array
Current (ARR) returns the value pointed to by the active pointer
Next returns the value that the next pointer points to
For example
Copy CodeThe code is as follows:
$array = Array (
' Fruit1 ' = ' apple ',
' Fruit2 ' = ' orange ',
' fruit3 ' = ' grape ',
' Fruit4 ' = ' apple ',
' Fruit5 ' = ' apple '
);
This cycle echoes all associative array
Key where value equals "apple"
while ($fruit _name = current ($array)) {
if ($fruit _name = = ' Apple ') {
echo Key ($array). '
';
}
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 cells 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 keep 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 "Focus 2"
trim (str) to eliminate whitespace on both sides of the string
RTrim ()
addslashes add \
strlen (str) to string length before specifying a predefined character
substr (str,start,length) intercepts characters of a specified length in a specified string
the strstr (str,search) function is used to get a substring that specifies the first occurrence of a string in another string to the end of the latter. The first occurrence of a character in the same
Strpos (str,search) lookup string as STRCHR
Str_replace (search,replace,str) string replacement
Ucfirst (str) Capitalizes the first character of the string
Strtolower lowercase
ucwords (str) capitalizes the first letter of each word in the string
strcmp (str1,str2) function to compare two strings
Substr_count The () function retrieves the number of occurrences of a substring
regular related string functions:
Preg_match (pattern,subject,matches) searches the subject string for content that matches the regular expression given by pattern. The results of the match are stored in matches, Matches[0] all matches, matches[1] The first mode unit MATCHES[1] The second mode unit, and so on.
Preg_match_all (pattern,subject,matches) global match, the remaining Preg_match functions are similar
Preg_replace (PATTERN,REPLACEMENT,STR) Perform search and replace of regular expressions, three [String,string][array,string][array,array]
Preg_split (PATTERN,STR) to split strings with regular expressions
Preg_grep ( Pattern,array) matches the value of an array with a regular expression, returning a new array information

Time "Focus 3"
PHP datetime function Date ()
1, year-month-day
echo Date (' y-m-j ');
2007-02-6
echo Date (' y-n-j ');
07-2-6
Uppercase Y represents the year four digits, while lowercase y represents the two digits of the year;
Lowercase m represents the number of months (with leading), while lowercase n indicates no leading month number.
echo Date (' y-m-j ');
2007-feb-6
echo Date (' y-m-d ');
2007-02-06
The uppercase M represents the 3 abbreviated characters of the month, while the lowercase m represents the number of the month (with leading 0);
J with no uppercase, only lowercase J indicates the date of the month, no leading o, or lowercase D if a month with a leading is required.
echo Date (' y-m-j ');
2007-feb-6
echo Date (' Y-f-js ');
2007-february-6th
Uppercase M represents the 3 abbreviated characters of the month, while uppercase F indicates the full written English of the month. (No lowercase f)
Uppercase S represents the suffix of the 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 in lowercase d and lowercase j, and uppercase s represents the suffix of the date.

2, Hours: minutes: seconds
By default, the PHP interpretation is displayed as "Greenwich Mean Time", which differs from our local time by 8 hours.
echo Date (' g:i:s a ');
5:56:57 am
echo Date (' H:i:s A ');
05:56:57 AM
The lowercase g represents a 12-hour system without a leading 0, while the lowercase h indicates a 12-hour system with a leading 0.
When using the 12-hour system, it is necessary to indicate that in the afternoon, lowercase a represents the lowercase "am" and "PM", and uppercase a denotes uppercase "AM" and "PM".
echo Date (' g:i:s ');
14:02:26
Uppercase G denotes 24-hour hours, but without preamble, and uppercase H for leading 24-hour hours
Summary:
The letter g means the hour without leading, the letter H denotes the hour with the preamble;
lowercase g, h for 12-hour system, uppercase G, h for 24-hour system.

3, Leap year, week, day
echo Date (' L ');
Leap Year: 0
echo Date (' L ');
Today is: Tuesday
echo Date (' D ');
Today is: Tue
Uppercase L Indicates whether this year is a leap years, a Boolean value that returns 1 for true, otherwise 0;
Lowercase L Indicates the day of the week in English full write (Tuesday);
Instead, use uppercase D to denote the 3-character abbreviation (TUE) of the day of the week.
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, and the numeric representation
Uppercase 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 indicates the current month and how many days
Lowercase z indicates that today is the first day of the year

4, other
echo Date (' T ');
Utc
Capital T indicates the server's time zone setting
echo Date (' I ');
0
Uppercase I indicates whether the current is daylight saving time, returns 1 for true, otherwise 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 timestamp 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 letter T is used to interval the date and time, the time format is HH:MM:SS, and the time zone is represented by the deviation of Greenwich Mean Time (GMT).
echo Date (' R ');
Tue, Feb 2007 14:25:52 +0000
The lowercase R represents the RFC822 date.
The small date () function shows the powerful function and charm of PHP, and then compares the ASP, hehe.
Checkdate ($month, $date, $year)
If the applied value constitutes a valid date, the function returns to true. For example, for the error date February 31, 2005, this function returns FALSE.
Use this function to check the date and make the date effective before the date is used to calculate or save in the database.
Copy CodeThe code is as follows:
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 the form of an array. Each element in the array represents a specific component of a date/time value. You can submit an optional time label argument to the function to get the date/time value that corresponds to the time label.
Apply this function to obtain a series of discrete, easy-to-detach date/time values.
Copy CodeThe code is as follows:
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)
This function has the opposite effect of getdate (): It generates a UNIX time label from a series of date and time values (the number of seconds elapsed from GMT January 1, 1970 to now). When no arguments are used, it generates the UNIX time label for the current time.
Use this function to get the UNIX time label for immediate time. This time label is often used in many database and program languages.
Copy CodeThe code is as follows:
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 artificially read. It is the most powerful function in the PHP date/Time API and can be used in a series of correction values to convert an integer time label into the desired string format.
This function is applied when the time or date is formatted for display.
Copy CodeThe code is as follows:
Format Current Date
Returns "13-sep-2005 01:16 PM"
echo Date ("D-m-y h:i A", Mktime ());
?>

Strtotime ($STR)
This function converts a human-readable English date/time string into a Unix time label.
Apply this function to convert a non-normalized date/time string into a standard, compatible UNIX time label.
Copy CodeThe code is as follows:
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 Days");
?>

strftime ($format, $ts)
As defined by the previous setlocale () function, this function formats the UNIX time label as a date string for the current environment.
Apply this function to establish a date string that is compatible with the current environment.
Copy CodeThe code is as follows:
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 for the current environment.
Apply this function to establish a date string that is compatible with the current environment.
Copy CodeThe code is as follows:
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 no arguments are used, it generates a UNIX time label for the current GMT time.
Use this function to get the UNIX time label for the GMT instant time.
Copy CodeThe code is as follows:
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 expressed in GMT (non-local time).
This function is applied when the time label is represented in GMT.
Copy CodeThe code is as follows:
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 date/Time function calls are set and the default time zone is restored.
Note: This function is only valid in PHP 5.1+.
This function is a convenient shortcut for setting the time zone for future operations.
Copy CodeThe code is as follows:
Set TimeZone to UTC
Date_default_timezone_set (' UTC ');
?>

Copy CodeThe code is as follows:
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")));
Number of days in the quarter not last month
$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 ')));

File Operations "Focus 4"
file_exists (filename) file or directory exists
FileSize (filename) Get 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";
echo $path _parts["basename"]. "\ n";
echo $path _parts["extension"]. "\ n";
/www/htdocsindex.htmlhtml
mkdir (dirname) Creating a directory
RmDir (dirname) Delete empty directory
fopen (filename, mode) Open file
Fclose (FP) close file
Fwrite (fp,str,length) write file
File_put_contents (filename,content) Save the content as a file
file_get_contents (filename) reads content from a file
Fread (fp,length) Read file
Fgets () reads a row from the file pointer
FGETC () reading characters from the file pointer
File () reads the entire document into an array, and each cell in the array is the corresponding row in the file
ReadFile () reads in a file and writes to the output buffer
Copy (filename1,filename2) copying files
Unlink (filename) Delete file
Rename (filename1,filename2) renaming a file or directory
$text = Iconv (' GBK ', ' utf-8 ', ' Chi-wai ');///Convert GBK encoding to Utf-8

http://www.bkjia.com/PHPjc/327876.html www.bkjia.com true http://www.bkjia.com/PHPjc/327876.html techarticle array: "Focus 1" implode (delimited, arr) connects array value data to specified characters such as: $arr =array (' 1 ', ' 2 ', ' 3 ', ' 4 '), $str =implode ('-', $arr); explode ([delimited], ARR) by specified rules ...

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