PHP Common functions

Source: Internet
Author: User

<?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 ("<br/>");//Returns the number of occurrences of the second argument string in the first string $int=substr_count ("Abcdeabcdeablkabd", "AB");// Returns the position of the second string at the first occurrence of the first string, 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");//Intercept the string $str=strstr ("Sdafsdgaababdsfgs", "AB") of 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 fill, 1 right start, 2 Both 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 a new array that consists of all the key values of the array in parentheses without changing the $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, the key value starts from 0 and the original array is unchanged $array=array_ Values ($arr);//Returns the key value pointed to by the current array pointer $key=key ($ARR);//returns the element value $value=current ($arr) that the current array pointer points to, or returns an array of the key values and element values of the current array pointer to the element. Then push the pointer to the next bit, the last pointer to an empty element returns an empty//returned array with four fixed key values corresponding to the element value is the key value of the return element and the element value, where 0, ' key ' key value corresponds to the return element key value, 1, ' value ' key value corresponds to the returned element value $array= each ($arr);//First push the array pointer to the next bit, thenReturns the element value $value=next ($arr) pointed to when the pointer is moved,//pushes the array pointer to the last bit, and then returns the element value $value=prev ($arr) that the pointer moved to, or the array pointer resets the pointer to the first element and returns the element value $value=reset ($arr);//To 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 to the end of the first parameter array, the index is counted from the smallest unused value, and the array length after the return $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 rejects the last element from the original array $value=array_pop ($arr);//array_pop instead, Extracts the value of an element in the header of an array, and rejects the $value=array_shift ($arr) from the original array, and the first parameter array reaches the second parameter value length, and the third parameter is added as an element to the end of the first parameter array. The index is calculated from the minimum unused value and returned, the original array does not change $array1=array_pad ($arr, ten, "T10");//Returns a new array that rejects the redundant elements of the original array, the original array does not change $array=array_unique ($ ARRAY1);//The original array key value is broken back to the value of the value of the aspartame code from small to large, the index from the number 0 to recalculate the $int=sort ($array);//And sort, in contrast to the element value Ascol the size of the value of a descending order, the index is re-counted from 0 $int=rsort ($array);//Returns an array of the values of each element in the first parameter array, followed by the value of the key to 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 invariant $array=array_merge ($arr, Array ("A", "B", "C"));//In the first parameter array from the second parameter value position to intercept to the third parameter numeric length of the array key value + Element and returns, the first element of the array is positioned from 0 $array=array_Slice ($arr, 2, 1);//intercept function and array_slice () just like the intercept part in the original array is removed $array=array_splice ($arr, 2, 1);//The first parameter as the first element, each increment of the value of the parameter three, Self-increment and then as an element exists in the array until the value reaches the value of the parameter two in the array and returns the array//parameter one, the parameter two can be a number, can be a single character, the individual characters are calculated according to Ascol code value, the third parameter does not write the default per self-increment 1$array=range (3,9,2); /The corresponding relationship between the original array element and corresponding key value is re-arranged to return the true and False $bool=shuffle ($arr);//computes the and $int=array_sum of all numeric element values in the array (array ("A", 2, "CSSF");// Splits an array into new array blocks, 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, and 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 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----property to access the Decode_arr = Json_decode ($encode _str); Var_dump ($decode _arr->k1);//If the second argument is true then automatically returns the array type, 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 described format, But the encoded string is not as readable as json_encode () encoded $STR = serialize ($arr); Var_dump ($STR);//similar to Json_decode (), serialize () The formatted string is converted back to the pre-formatted variable and is converted back to the original mode without passing the second parameter $arr = Unserialize ($STR); Var_dump ($arr);? >

Common PHP functions

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.