PHP Common functions

Source: Internet
Author: User
Tags first string string back

<?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 a few 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") before the execution of the PHP program will be read into the Require designated import files, such as 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 and the elements are displayed in turn//=============================== 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 character before and after the interception, if the specified character at the beginning or end of the returned array at the beginning or end of the element is an empty string//not split into a string to return to the array corresponding element a null value. 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, remove the space at the beginning of the left and replace the string in the second argument$str =LTrim ("A ASD", "a");//Remove whitespace from the right of the string$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 remove the space between the beginning of the string$str =Trim ("Sdsdfas", "a");//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. The last character at the end counts-1, and the intercept direction is always 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");//Same as str_replace usage, except case-insensitive//$str =str_ireplace ("A", "" "," ABCABCABCA ");//String that returns the character of the string in parentheses in all uppercase$str =Strtoupper ("Sdaf");//Returns the first string in parentheses after it becomes uppercase$str =Ucfirst ("Asdf");//Use Echo to print the bracketed string in parentheses, 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 0$int =Strpos ("Asagaab", "AB");//Returns the position of the second string at the last occurrence of the first string, the first character position 0$int =Strrpos ("Asagaabadfab", "AB");//Intercepts a string that returns the first argument from left to right in the return parameter one to the last character of the argument$str =Strstr ("Sdafsdgaababdsfgs", "AB");//Intercepts a string that returns the last argument from left to right in the return parameter one to the last character of the argument$str =STRRCHR ("Sdafsdgaababdsfgs", "AB");//Add "\" to each character in parameter two before the same character in parameter one$str =Addcslashes ("Abcdefghijklmn", "AKD");//Fills the string of parameter one to the length specified in parameter two (number of single characters), the 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 to the right, and 22 sides at the same time. Do not write default at the right start padding$str =Str_pad ("Abcdefgh", Ten, "at", 0);//Compare the two strings corresponding to the character of the Andean 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 0$int 1 =strcmp ("B", "a");//Return the first parameter after formatting the 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 three parameters are not written, the default minus the fractional part, integer every three bits with commas, split. 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 first parameter element in the array element of the second argument$bool =In_array ("B",$arr);//Returns a new array of all the key values in parentheses that do not change the original array$array =Array_keys ($arr);//Determines whether the second parameter's array has the first parameter's key value, returns the True or 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 and the original array unchanged$array =Array_values ($arr);//Returns the key value pointed to by the current array pointer$key =Key$arr);//Returns the element value pointed to by the current array pointer$value =Current$arr);//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 ' key value corresponds to the returned element value$array =each$arr);//First, the array pointer is pushed to the next bit, and then the value of the element pointed to after the pointer moves$value =Next$arr);//Pushes the array pointer to the previous bit and returns the value of the element pointed to when the pointer moves$value =prev ($arr);  resets the array pointer to the first element and returns the element value $value =Reset ($arr);  points The array pointer to the last element and returns 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 is counted from the smallest unused value, 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 from the header of an array and rejects an element from the original array.$value =Array_shift ($arr);//Let the first parameter array reach the second parameter value length, add the third argument as an element to the end of the first parameter array, the index is counted from the minimum unused value, and the original array does not change$array 1 =Array_pad ($arr, ten, "T10.");//Returns a new array that rejects the extra repeating elements in the original array, without changing the original array$array =Array_unique ($array 1);//Break the original array key value to re-order the value of the value of the aspartame code from small to large, the index from the number 0, the re-meter$int =Sort$array);//As opposed to sort, Ascol the value of the element value in descending order of size, the index is recalculated 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);//Combines two numbers and returns the original array unchanged$array =Array_merge ($arr,Array ("A", "B", "C"));//In the first parameter array, starting from the second parameter value position, intercept the array key value of the third parameter value length + element and return, array first element position from 0$array =Array_slice ($arr, 2,1);//The interception function is just like array_slice (), except that the intercept is removed from the original array.$array =Array_splice ($arr, 2,1);//The first parameter as the first element, each increment of the value of the parameter three, and then as an element in the array, until the value reaches the value of the parameter two is stored in the array and return the array//parameter one, the parameter two can be a number, can be a single character, the individual characters according to Ascol code value calculation, The third parameter does not write default 1 per increment$array =Range (3,9,2);//The corresponding relationship between the original array element and corresponding key value is re-randomly returned to true or false.$bool =Shuffle$arr);//Computes the value of all numeric elements in the array and$int =Array_sum (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 within each element 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, false by default is not preserved$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 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---Properties to access$decode _arr = Json_decode ($encode _str);Var_dump ($decode _arr->K1);//If the second argument is true, the array type is returned automatically, not the second parameter defaults to False$decode _arr = Json_decode ($encode _str,True);Var_dump ($decode _arr); similar to Json_encode (), the passed parameters are serialized in the format described, but the encoded string is not as readable as the Json_encode () encoded $STR = Serialize ($arr);  Var_dump ($str);  similar to the Json_decode () function, converts the serialize () formatted string back to the pre-formatted variable and converts it back to the original mode without passing the second argument $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.