Some common function collections in PHP

Source: Internet
Author: User
Tags array length bool empty first string numeric json sort split

This article mainly introduces some common function collection in PHP, this article collects some time date, output print, common string function, commonly used array method, need friend can refer to the following

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179-18 0 181 182 183 184 185 186 187 188 189 190 <?php//=============================== Time Date ===============================//y returns the last two digits of the year, Y-year four-digit, M-month-number, M-month English. D month number, D-week English $date =date ("y-m-d"); $date =date ("y-m-d h:i:s");//With Time  //include,include_once.require,require_once//require ("file.php") Before the PHP program is executed, it will read the file introduced by the require, if the error is fatal. Include ("file.php") can be placed in any location in the PHP program, the PHP program is executed to read into the include specified the introduction of the file, such as error will prompt  //=============================== Output print ===============================//sprintf ("%d", "3.2");//Only format, return formatted string, not output. printf ("%d", "3.2");/is formatted and output//print ("3.2");//Output only//echo "Nihao", "AA";//can output multiple strings//print_r (Array ("A", "B", "C")) //To display the array's key values and elements in turn  //=============================== common String Functions ===============================  //Get string length, How many characters, the space is also counted $str = "Sdaf SD"; $len =strlen ($STR);  //using the string in the first argument, concatenate each element in the following array and return a string. $str =implode ("-", Array ("A", "B", "C"));  //String segmentation method, returns an array, using the characters in the first argument to split the following string, the specified character is intercepted both before and after, if the specified character at the beginning or end of the returned array of elements at the beginning or end of an empty string// Returns an empty value to the corresponding element of an array without dividing it into a string. The last restriction returns the length of the array, without restriction, and is separated. $array =EXPLOde ("A", "Asddad addsadassd dasdadfsdfasdaaa", 4); Print_r ($array);  //Remove the space at the beginning of the string and return///If the second argument is to remove the space at the beginning of the left and replace the string in the second argument $str =ltrim ("A ASD", "a");  //Remove the space at the right beginning of the string $str =rtrim ("ASD");  //Remove the string at the beginning of the first string with the second argument. If there is no second parameter, the blank space at the beginning of the string is eliminated by default $str =trim ("Sdsdfas", "a");  //How long (how many) characters to start at the specified position in the first argument of the string, and the first character position in the string is counted from 0. If the second argument is negative, the string is taken from the bottom of the string at the beginning of the first number. At the end of the last character count-1, The intercept direction always from left to right $str =substr ("Abcdefgh", 0,4);    //Replace the first argument string of the third argument with a parameter two string $str =str_replace ("A", "", "ABCABCABCA"); Same as str_replace usage, only case-insensitive//$str =str_ireplace ("A", "", "ABCABCABCA");  //Returns a string of characters in parentheses with all uppercase strings $str =strtoupper ("Sdaf");  //Turn the first string in parentheses into uppercase and return $str =ucfirst ("asdf");  //with ECHO, etc. to print the parentheses in the string of strings on the page, the original print out the parentheses in the string, including the sign $str =htmlentities ("<br/>");  //Returns the number of occurrences of the second argument string in the first string $int =substr_count ("Abcdeabcdeablkabd", "AB");  //Returns the second string at the first occurrence of the first string, the position of the first character is 0 $int =strpos ("Asagaab", "AB");  //Returns the second string at the last occurrence of the first string, the position of the first character is 0 $int =strrpos ("Asagaabadfab", "AB");  //interceptionReturns the first argument from left to right in parameter two to the string of the last character of the argument $str =strstr ("Sdafsdgaababdsfgs", "AB");  //Intercept return parameter one from left to right the last occurrence of the argument two to the parameter of the last character string $str =strrchr ("Sdafsdgaababdsfgs", "AB");  //Add "" $str =addcslashes ("Abcdefghijklmn", "AKD") to each character in parameter two before the same character in parameter one;  //The string of parameter one is filled to the length specified by parameter two (single character count), parameter three is the specified padding string, no default space//parameter four fill position, 0 is in the left of parameter one padding, 1 right beginning, 22 side opening at the same time. Do not write default padding $str =str_pad ("Abcdefgh", "at", 0) in the right opening;  //To compare two strings corresponding to the character of the Andes code value, the first pair is not the same, if the parameter is greater than two in the return of 1, conversely return-1, two strings exactly the same return 0 $int 1=strcmp ("B", "a");  //Returns the number format after the first parameter is formatted, the second parameter is to keep a few decimal points, parameter three is to replace the decimal point for the parameter three, parameter four is the integer part every three bits with what character split//After three parameters are not written, the default minus the decimal part, integer every three bits with commas, split. Parameter four must exist at the same time $str =number_format (1231233.1415,2, "D", "a");  //=============================== Common array methods ===============================   $arr =array ("K0" => "a", "K1" => "B", "K2" => "C");  //Returns the number of array elements $int =count ($arr);  //Determine whether the first parameter element in the array element of the second parameter $bool =in_array ("B", $arr);  //Returns a new array of all the key values in parentheses, the original array does not change $array =array_keys ($arr);  //Determine whether the first parameter's key value is in the array of the second parameter, and return 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 values starting from 0 and the original array unchanged $array =array_values ($arr);  //Returns the key value $key =key ($arr) to which the current array pointer is pointing;  //Returns the element value that the current array pointer points to $value =current ($arr);  //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 one, and the last pointer to an empty element returns an empty//returned array with four fixed key values corresponding to the key value and element value of the returned element, of which 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 push the array pointer to the next, then return the element value pointed to after the pointer moves $value =next ($arr);  //Push the array pointer to the previous bit, then return the element value pointed to after the pointer moves $value =prev ($arr);  //Let the array pointer reset point 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 first parameter to the end of the first parameter array as an element, the index begins with the smallest unused value, and the length of the array after the return $int =array_push ($arr, "D", "DFSD");  //Add all the arguments after the first argument array as elements to the beginning of the first argument array, with the key value 0 from the first element, the original Non-numeric key value remains unchanged, the original element sort position is unchanged, and the array length after the return $int =array_unshift ($arr , "T1", "T2");  //Return extract the last element value from the end of the array and remove the last element from the original array $value =array_pop ($arr);  //array_pop Instead, extract the value of the first element of the returned array and remove the head from the original array $value =array_shift ($arr);  //Let the first parameter array reach the second parameter numeric length, add the third parameter as an element to the end of the first parameter array, and return the index from the smallest number of values, and the original array does not change $array 1=array_pad ($arr, "T10");  //returns a new array that removes the extra repeating elements from the original array, and the original array does not change $array =array_unique ($array 1);  //The original array key value is broken back to the element value of the Apache code value from small to large sort, the index begins with the number 0 recosting $int =sort ($array);  //In contrast to sort, reorder by element value Ascol code value size, indexed from 0 $int =rsort ($array);  //Returns an array of two array of parameters, in which each element value in the first parameter array is followed as the key value. The length of the two arrays must be the same, and 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 argument array from the second parameter numeric position to intercept the array key value + element of the numeric length of the third parameter and return, the first element of the array is positioned from 0 $array =array_slice ($arr, 2, 1); The  //interception function is the same as Array_slice (), except that the interception part is excluded from the original array $array =array_splice ($arr, 2, 1);  //The first argument as the first element, each time the value of the parameter three, since the increment and then as an element in the array, until the value reached the value of parameter two to save in the array and return the array//parameter one, parameter two can be a number, can be a single character, the first word by Ascol code value, The third parameter does not write the default 1 $array =range (3,9,2) per self increase;  //The corresponding relationship between the original array elements and corresponding key values is returned randomly to return the true $bool =shuffle ($arr);  //Computes and $int =array_sum (Array ("A", 2, "CSSF") of all numeric element values in the array;    //dividing an array into a new array block, each element of the new array is an array, and a few elements within each element of the new array are determined by the parameter II//third argument whether the key value of the element retains the original key value or not, and true is reserved, and the default false is not preserved $array =array_chunk (Array ("A" => "a", "B", "C", "D", "E", "F", "G", "H"), 2,true);    //json_encode () converts an array to a JSON format string returns the $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 a number of groups, which must be enclosed in quotes in a JSON format string with double quotes $decode _arr = (array) Json_decode ($ ENCODE_STR); 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.