Common functions in PHP collection _ php instances

Source: Internet
Author: User
This article mainly introduces some common functions in PHP. This article collects some time and date, output printing, common string functions, and common array methods. For more information, see
<? Php // ========================== ============================================/// y returns the last two digits of the year, y-year four-digit, m-month number, M-month English. D number of months, D number of weeks $ date = date ("Y-m-d"); $ date = date ("Y-m-d H: I: s "); // with hour, minute, and second // include, include_once.require, require_once // require (" file. php ") before executing the PHP program, it will first read the file specified by require. If an error occurs, it is fatal. // Include ("file. php ") can be placed in any position of the PHP program. only when the PHP program is executed can the file specified by include be read, if an error occurs, the following message is displayed: // ========================================== output print. ====================================/// sprintf ("% d ", "3.2"); // only format, returns the formatted string, not output. // Printf ("% d", "3.2"); // format and output // print ("3.2 "); // output only // echo "nihao", "aa"; // multiple strings can be output // print_r (array ("a", "B ", "c ")); // display the key and element of the array in sequence. ===== common string functions ======================================================== // get the string length, the number of characters and spaces are calculated as $ str = "sdaf sd"; $ len = strlen ($ str); // use the string in the first parameter, concatenates each element in the following array and returns a string. $ Str = implode ("-", array ("a", "B", "c"); // returns an array, use the characters in the first parameter to separate the strings that follow the specified characters, and intercept both before and after the specified characters, if the specified character starts or ends, the element at the beginning or end of the returned array is an empty string // if it is not split into strings, a null value is returned to the corresponding element of the array. The last limit returns the length of the array. if there is no limit, the array will be split. $ Array = explode ("a", "asddad addsadassd dasdadfsdfasdaaa", 4); // print_r ($ array); // removes spaces starting from the left of the string, return // if the second parameter exists, replace the space at the beginning of the left with the string $ str = ltrim ("a asd", "a") in the second parameter "); // remove the spaces starting with the right of the string $ str = rtrim ("asd"); // remove the strings starting with the second parameter on both sides of the first string. If there is no second parameter, spaces starting with both sides of the string $ str = trim ("sdsdfas", "a") are removed by default "); // the length (number of characters) starting from the specified position in the first parameter of the string. the position of the first character in the string is counted from 0. // If the second parameter is negative, the length of the string starting from the last few to the end of the string. The last character ends with-1. the truncation direction is always from left to right $ str = substr ("abcdefgh ); // replace the first parameter string of the third parameter with the string of the second parameter $ str = str_replace ("a", "", "abcabcAbca"); // use the same method as str_replace, only case insensitive // $ str = str_ireplace ("a", "", "abcabcAbca "); // returns the string $ str = strtoupper ("sdaf") with all uppercase characters in the brackets "); // convert the first string in the brackets into uppercase and return $ str = ucfirst ("asdf "); // Print the strings in the brackets in the original style on the webpage using echo and so on, including the tag character $ str = htmlentities ("
"); // Returns the number of times the second parameter string appears in the first string $ int = substr_count (" abcdeabcdeablkabd "," AB "); // returns the position where the second string appears for the first time. The first character is 0 $ int = strpos ("asagaab", "AB "); // returns the position of the second string at the last occurrence of the first string. The first character is 0 $ int = strrpos ("asagaabadfab", "AB "); // extract the string $ str = strstr ("sdafsdgaabdsfgs", "AB") that appears from the left to the right of the return parameter 2 to the last character of the parameter 1 "); // Intercept the string $ str = strrchr ("sdafsdgaabdsfgs", "AB") that appears from the left to the right of the return parameter 2 to the last character of the parameter 1 "); // add "\" $ str = addcslashes (" Abcdefghijklmn "," akd "); // fill the string of parameter 1 with the length specified by parameter 2 (number of single characters), and parameter 3 with the string of the specified filling, do not enter the default Space. // The four filling positions of the parameter. 0 is filled at the beginning of parameter 1 on the left, 1 at the right, and 2 at the same time. If this parameter is left blank, $ str = str_pad ("abcdefgh", 10, "at", 0) is filled at the beginning of the right by default. // compare the two strings with the corresponding ARSK code values in sequence, the first pair is different. if the value 1 in parameter 1 is greater than the value 1 in parameter 2, the value-1 is returned, and the value 0 $ int1 = strcmp ("B ", "a"); // return the number format after the first parameter is formatted. The second parameter is to retain a few decimals. parameter 3 is to replace the decimal point with parameter 3, parameter 4 is the character used to separate each three digits of the integer part. // if no value is specified for the next three parameters, the fractional part is removed by default. the integer is separated by commas (,) at every three digits. Parameter 3, parameter 4 must both exist $ str = number_format (1231233.1415, 2, "d", ""); // ================================ common array methods ==== ===========================$ arr = array ("k0" => "", "k1" => "B", "k2" => "c"); // returns the number of array elements $ int = count ($ arr ); // Determine whether the first parameter element $ bool = in_array ("B", $ arr) exists in the array element of the second parameter ); // return the new array consisting of all the key values in the array. the original array does not change $ array = array_keys ($ arr ); // check whether the key value of the first parameter exists in the array of the second parameter. $ bool = array_key_exists ("k1", $ arr) is returned ); // return a new array consisting of all element values in the original array. The key value starts from 0 and the original array remains unchanged $ array = arra Y_values ($ arr); // returns the key value $ key = key ($ arr) pointed to by the current array pointer ); // returns the element value $ value = current ($ arr) pointed to by the current array pointer; // returns an array consisting of the key value and element value of the element pointed to by the current array pointer, push the pointer to the next position, finally, the pointer points to an empty element and returns null // The element values corresponding to four fixed key values in the returned array are the key values and element values of the returned element, where 0, the 'key' key values correspond to the returned element key values. 1 and 'value' key values all correspond to the returned element values $ array = each ($ arr ); // first push the array pointer to the next position, and then return the element value $ value = next ($ arr) pointed to after the pointer moves; // pushes the array pointer to the last position, returns the element value $ value = prev ($ arr) pointed to after the pointer is moved ); // reset the array pointer to the first element and return the element value $ value = reset ($ arr); // point the array pointer to the last element, returns the value of the last element $ value = end ($ Arr); // add parameters after the first parameter to the end of the first parameter array as elements. The index starts from the smallest unused value, the returned array length $ int = array_push ($ arr, "d", "dfsd "); // add all parameters after the first parameter array as elements to the beginning of the first parameter array, and the key value is accumulated from the first element with 0, the key value of the original non-numeric value remains unchanged, and the sorting position of the original element remains unchanged. the returned array length $ int = array_unshift ($ arr, "t1", "t2 "); // extract the last element value from the end of the array and remove the last element from the original array $ value = array_pop ($ arr); // opposite to array_pop, extract an element value from the array header and remove it from the original array. $ value = array_shift ($ arr); // set the length of the first parameter array to the value of the second parameter, add the third parameter as an element to the end of the first parameter array. The index starts from the minimum unused value and returns the result. the original array is not Change $ array1 = array_pad ($ arr, 10, "t10"); // return a new array that removes unnecessary duplicate elements from the original array, the original array does not change $ array = array_unique ($ array1); // breaks the key value of the original array and sorts it from small to large, the index starts from the number 0 and recalculates $ int = sort ($ array); // opposite to sort, the index is re-ordered in descending order of the element value askecode value, the index is recalculated from 0 $ int = rsort ($ array); // returns the array that uses each element value in the first parameter array as the key value to the array of parameter 2, the two arrays must have the same length. the original array does not change $ array = array_combine (array ("a", "B", "c", "d", "e "), $ arr); // merge the two arrays and return the original array unchanged $ array = array_merge ($ arr, array ("a", "B", "c ")); // in the first parameter array, truncate from the second parameter value position The array key value + element of the length of the three parameter values are returned. The first element position of the array is calculated from 0. $ array = array_slice ($ arr ); // the truncation function is the same as that of array_slice (). it only removes $ array = array_splice ($ arr,) from the original array. // The first parameter is used as the first element, each time the value of the auto-increment parameter 3 is added and then stored as an element in the array until the value reaches the value of the parameter 2 is saved in the array. then, this array // parameter 1 is returned, parameter 2 can be a number or a single character. a single character is calculated based on the Astic code value. if the third parameter is left blank, the value is automatically increased by 1 $ array = range (, 2 ); // re-Randomly arrange the correspondence between the original array element and the corresponding key value to return the true and false $ bool = shuffle ($ arr ); // calculate the sum of all numeric element values 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 each element of the new array is an array. Several elements in the element are determined by parameter 2 // The third parameter determines whether the key value of the element is retained. if it is true, the original key value is retained, $ array = array_chunk (array ("a" => "a", "B", "c", "d", "e", "f ", "g", "h"), 2, true); // json_encode () convert the array to a JSON string and return $ arr = array ('k1 '=> 'val1', 'K2' => 'val2 ', 'k3 '=> array ('v3', 'V4 '); echo $ encode_str = json_encode ($ arr); // json_decode () convert a JSON string to an array object and return it. double quotation marks ($ decode_arr = (array) json_decode ($ encode_str) must be used to enclose the key and value in a JSON string ); var_dump ($ decode_arr);?>

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.