Source: http://www.cnblogs.com/dreamhome/archive/2011/05/04/2037011.html
----------------------------
<?php//=============================== Time Date ===============================//y returns the last two digits of the year, Y year four digits, m month number, M month English. D Month Date number, D week several English $date=date ("y-m-d"), $date =date ("y-m-d h:i:s");//With time division seconds//include,include_once.require,require_once// Require ("file.php") will be read into the file specified by require before the PHP program executes, if the error is fatal. The Include ("file.php") can be placed anywhere in the PHP program, the PHP program will be read into the include specifies the introduction of the file, such as error will prompt//=============================== output print = = =============================
sprintf ("%d", "3.2");//format only, return formatted string, not output. printf ("%d", "3.2");//format, Output//print ("3.2");//Output only//echo "Nihao", "AA";//can output multiple strings//print_r (Array ("A", "B", "C")) ;//The key value of the array is displayed in turn with the elements//=============================== commonly used string functions ===============================//get the string length, how many characters, the space is also counted $ str= "Sdaf SD"; $len =strlen ($STR);//Use the string in the first argument to concatenate each element in the following array and return a string. $str =implode ("-", Array ("A", "B", "C"));//String segmentation method, return an array, with the characters in the first argument to split the following string, the specified characters are intercepted before and after, If the specified character at the beginning or end of the returned array begins or ends with an empty string///not split into a string, returns an empty value to the corresponding element of the array. The last limit returns the length of the array, which is not limited, and is always split down. $array =explode ("A", "Asddad addsadassd dasdadfsdfasdaaa", 4);//print_r ($array);//Remove the space at the beginning of the string and return// If there is a second parameter is to reject the left-hand side of the space to remove the second parameter in the string $str=ltrim ("A ASD", "a");//Remove the string to the right of the beginning of the space $str=rtrim ("ASD");// Rejects the string that begins with the second argument on both sides of the first string. If there is no second argument, the default is to reject the space $str=trim ("Sdsdfas", "a") that begins at the beginning of the string, and/or how long (how many) characters are taken from the specified position in the first argument of the string, and the first character position in the string is calculated from 0. If the second argument is negative, the string is taken from the end of the string to the beginning of the last number. At the end of the last character count-1, the interception direction is always from left to right $str=substr ("Abcdefgh", 0,4);//Replace the first argument string of the third argument with the parameter two string $str=str_replace ("a", "", "" ABCABCABCA ");//with str_replace usage, except case-insensitive//$STR =stR_ireplace ("A", "" "," ABCABCABCA ");//Returns the string $str=strtoupper (" Sdaf ") of all uppercase characters in parentheses, and returns the $str=ucfirst after the first string in parentheses becomes uppercase (" Asdf ");//use echo to print the bracketed string in parentheses, including the label Signature $str=htmlentities (" <br/> "), or the number of occurrences of the second argument string in the first string. Int=substr_count ("Abcdeabcdeablkabd", "AB");//Returns the second string where the first string appears first, the first character position is 0$int=strpos ("Asagaab", "AB");// Returns the position of the second string at the last occurrence of the first string, the first character position is 0$int=strrpos ("Asagaabadfab", "AB"), or the string that returns the first parameter from left to right in the return parameter one to the last character of the argument $str= Strstr ("Sdafsdgaababdsfgs", "AB");//Intercept the string $STR=STRRCHR ("Sdafsdgaababdsfgs", "AB") that returns the last argument from left to right in the return parameter one to the last character of the argument ;//The parameter two each character in the parameter one in the same character before the "\" $str =addcslashes ("Abcdefghijklmn", "AKD");//The string of parameter one is filled to the length specified by the parameter (number of single characters), the parameter three is the specified padding string, Do not write default space//parameter four fill position, 0 on the left of parameter one fill, 1 right start, 22 side start at the same time. Do not write default on the right to the beginning of the fill $str=str_pad ("Abcdefgh", Ten, "at", 0);//Compare two strings corresponding to the character of the Andes code value, the first pair is not the same, if the parameter one is greater than the return 1 in the parameter two, and vice versa returns-1, the two strings return exactly the same INT1=STRCMP ("B", "a");//Returns the first parameter formatted number format, the second parameter is to retain a few decimals, the parameter three is to change the decimal point to the parameter three, the parameter four is the integer part every three bits with what character division//After the three parameters are not written, the default minus the fractional part, Integers are separated by commas, every three bits. Parameter three, parameter four must exist simultaneously $str=number_format (1231233.1415,2, "D", "a");//=============================== Common Array Method =============================== $arr =array ("k0" = "a", "k1" = "B", "K2" and "C");// Returns the number of array elements $int=count ($arr);//Determines whether the array element of the second parameter has the first parameter element $bool=in_array ("B", $arr);//Returns a new array of the array's key values in parentheses without changing the original array $array= Array_keys ($arr);//Determines whether the second parameter in the array has the key value of the first parameter, returns the True and False $bool=array_key_exists ("K1", $arr);//Returns a new array of all the element values in the original array, with the key value starting from 0. The original array is invariant $array=array_values ($arr);//returns the key value $key=key ($arr) that the current array pointer points to, or returns the element value $value=current ($arr) that the current array pointer points to; Returns an array of the current array pointers to the element's key and element values, then pushes the pointer to the next bit, and the last pointer to an empty element that returns an empty//returned array with four fixed key values corresponding to the element values that correspond to the key and element values of the returned element, where 0, the ' key ' key value corresponds to the return element key The value of the ' value ' key corresponds to the returned element value $array=each ($arr);//The array pointer is first pushed to the next bit, and then the element value $value=next ($arr) pointed to after the pointer is moved, or the array pointer is pushed to the previous bit, and then the value of the element pointed to after the pointer moves is returned. Value=prev ($arr);//Let the array pointer reset to the first element and return the element value $value=reset ($arr);//point the array pointer to the last element and return the last element value $value=end ($arr);// Appends the argument of the first parameter as an element to the end of the first parameter array, the index starts with the smallest unused value, and returns the array length after $int=array_push ($arr, "D", "DFSD");// Add all parameters after the first parameter array as elements to the beginning of the first parameter array, the key value is re-accumulated from the first element at 0, the key value of the original non-numeric value remains unchanged, the original element sort position is unchanged, and the array length after the return $int=array_unshift ($arr, "T1", " T2 ");//Returns the last element value extracted from the end of the array and removes the last element from the original array $value=array_pop ($arr)//array_pop Instead, extracts the value of an element that returns an array header and rejects $value=array_shift ($arr) from the original array, and the first parameter array reaches the second parameter value length, Adding the third parameter as an element to the end of the first parameter array, the index is calculated from the smallest unused value and returned, and the original array does not change $array1=array_pad ($arr, ten, "T10");//Returns a new array that rejects the extra repeating elements in the original array, The original array does not change $array=array_unique ($array 1);//Break the original array key values from small to large by the value of the value of the $int=sort, and the index starts with the number 0 ($array);//In contrast to sort, The index is reordered in descending order of the element value Ascol code value, which is recalculated from 0 $int=rsort ($array);//Returns an array of the values of each element in the first parameter array to be paid as the value of the parameter two arrays, the length of the two arrays must be identical, the original array does not change $array=array_ Combine (Array ("A", "B", "C", "D", "E"), $arr);//combine two numbers and return the original array unchanged $array=array_merge ($arr, Array ("A", "B", "C"));// In the first parameter array, from the second parameter value position, the array key value of the third parameter value length is truncated and the element is returned, the first element of the array is positioned from 0 $array=array_slice ($arr, 2, 1);//intercept function and Array_slice (), Just remove the intercept part in the original array $array=array_splice ($arr, 2, 1);//The first parameter as the first element, each increment of the value of parameter three, the increment and then as an element in the array, until the value reaches the value of parameter two in the array and returns the array Parameter one, the parameter two can be a number, can be a single character, the Ascol code value, the third parameter does not write the default each time self-increment 1$array=range (3,9,2);//The original array element and corresponding key value of the corresponding relationship re-randomly return to true and false $bool=shuffle ($arr);//computes and $int=array_sum (array ("A", 2, "CSSF") of the values of all numeric elements in the array;//splits an array into a new array block, each element of the new array is an array, and the new array has several elements in each element that are determined by parameter two. /The third parameter determines whether the key value of the element retains the original key value is not writable, true is reserved, the default false does not preserve $arraY=array_chunk (Array ("a" = "a", "B", "C", "D", "E", "F", "G", "H"), 2,true);//json_encode () converts the array to a JSON-formatted string to return $arr = Array (' k1 ' = ' val1 ', ' k2 ' = ' val2 ', ' K3 ' =>array (' v3 ', ' v4 ')), echo $encode _str = Json_encode ($arr);//json_ Decode () converts a JSON format string to an object or an array returned, the return type is determined by the second parameter, the default value of the second parameter False,json the key and value in the format string must be enclosed in double quotation marks//If the return type is an object, The element or array that corresponds to a key value in the first cast of the array, the array is still cast before the array that corresponds to the key value is used. JSON multidimensional arrays and so on $decode_arr = (array) json_decode ($encode _str); Var_dump ($decode _arr);
or without casting, use the format of the object-and property to access $decode_arr = Json_decode ($encode _str); Var_dump ($decode _arr->k1);
If the second argument is true then the array type is returned automatically, without the second parameter default to False$decode_arr = Json_decode ($encode _str, True); Var_dump ($decode _arr);// Similar to Json_encode (), the passed arguments are serialized in the format described, but the encoded string is not as readable as json_encode () encoded $STR = serialize ($arr); Var_dump ($STR);//With Json_ Decode () functions Similarly, converts a serialize () formatted string back to a variable before the format, and does not pass the second parameter to convert the original mode back to $arr = Unserialize ($STR); Var_dump ($arr);? >
(go) PHP common functions