Some common functions in PHP collection, PHP common function collection
<?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 argument, remove the space at the beginning of the left to remove the string $str=ltrim ("A ASD", "a") in the second argument, or the space $str=rtrim ("ASD") that starts at the right of the string;// 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 "), or//with str_replace usage, except case-insensitive//$str =str_ireplace (" A "," "", "ABCABCABCA");//Returns the string of characters in parentheses with all uppercase strings $str= Strtoupper ("Sdaf");//returns $str=ucfirst ("Asdf") when the first string in parentheses is capitalized, and//uses Echo to print the bracketed string in parentheses when it is printed on the Web page, including the label signature $str= Htmlentities ("
");//Returns the number of times that the second argument string appears in the first string $int=substr_count (" Abcdeabcdeablkabd "," AB ");//Returns the second string where the first string appears first, the first character position is calculated 0$ Int=strpos ("Asagaab", "AB");//Returns the position of the second string at the last occurrence of the first string, the position of the first character is 0$int=strrpos ("Asagaabadfab", "AB");// Intercept the string $str=strstr ("Sdafsdgaababdsfgs", "AB") that returns the first parameter from left to right in the return parameter one to the last character of the argument. Intercepts the string $str=strrchr ("Sdafsdgaababdsfgs", "AB") of the last character of the argument from left to right in the return parameter one, and/or adds "\" $str to each character in parameter one before the same character in parameter a: Addcslashes ("Abcdefghijklmn", "AKD");//The string of parameter one is filled to the length specified by parameter two (single character number), parameter three is the specified padding string, does not write the default space//parameter four fill position, 0 on the left side of the parameter to fill the beginning, 1 Right start, 22 sides 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 an array in parenthesesThe original array of all the key values consists of a new array that does not change $array=array_keys ($arr);//Determines whether the second parameter 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 element values in the original array, with the key value starting from 0 and the original array unchanged $array=array_values ($arr);//returns the key value $key=key ($arr) that the current array pointer points to, or returns the element value pointed to by the current array pointer $value =current ($arr);//returns the current array pointer to an array of key values and element values of the element, then pushes the pointer to the next bit, and the last pointer to an empty element returns an empty//returned array with four fixed key values corresponding to the element values that are the key value of the returned element and the element value, where 0, ' Key ' key value corresponds to the return element key value, 1, the ' value ' key value corresponds to the returned element value $array=each ($arr);//First the array pointer to the next bit, and then return the pointer to the element value $value=next ($arr); Pushes the array pointer to the previous bit and returns the element value $value=prev ($arr) that the pointer moved to, and//lets 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);//Append the parameter after the first argument as an element into 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 from the header of the array and rejects the element from the original array $value= Array_shift ($arr);//Let the first parameter array reach the second parameter value length, add the third parameter as an element to the end of the first parameter array, and return the index from the smallest unused value, the original array does not change $array1=array_pad ($arr, "T10");//returns an extra repeating element in the original arrayA new array is removed, the original array does not change $array=array_unique ($array 1);//Break the original array key value to the value of the value of the same as the size of the aspartame code from small to large, the index starts with the number 0 $int=sort ($array);//And 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 retain $array=array_chunk (array ("a" = "a", "B", "C", "D", "E", "F", "G", "H"), 2, true);//json_encode () converts an array into 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 that can cast an array of values, which must be enclosed in double quotation marks when the key and value in the JSON format string are required to be quoted $decode_arr = (array) JSON _decode ($encode _str); Var_dump ($decode _arr);? >
http://www.bkjia.com/PHPjc/1006116.html www.bkjia.com true http://www.bkjia.com/PHPjc/1006116.html techarticle some common functions in PHP collection, PHP common function collection php//=============================== time Date ===============================//y return the last two bits of the year, Y year four digits, ...