(go) PHP common functions

Source: Internet
Author: User
Tags first string string back
Source: http://www.cnblogs.com/dreamhome/archive/2011/05/04/2037011.html

----------------------------

 Require,require_once//require("file.php") is read before the PHP program executesrequireThe specified imported file, if an error occurs, 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 parameter is to reject the left-hand side of the space to remove the second parameter in the string $str=ltrim ("A ASD", "a");//Remove the string to the right of the beginning of the space $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 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 ");//with str_replace usage, except case-insensitive//$STR =stR_ireplace ("A", "" "," ABCABCABCA ");//Returns the string $str=strtoupper (" Sdaf ") of all uppercase characters in parentheses, and returns the $str=ucfirst after the first string in parentheses becomes uppercase (" Asdf ");//use echo to print the bracketed string in parentheses, 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);//The array pointer is first pushed to the next bit, and then returnedPointer MovementThe value of the element that is pointed to is $value=next ($arr);//pushes the array pointer to the last bit and returnsPointer MovementThe element value $value=prev ($arr) that is pointed to after the array pointer resets 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); /Append the parameter after the first argument as an element into the end of the first parameter array,IndexStarting with the smallest unused value, return 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, and the key values are re-accumulated at 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 is $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 that returns an array header and strips the head of 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,IndexStarting from the smallest unused value and returning, the original array does not change $array1=array_pad ($arr, ten, "T10");//Returns a new array that rejects the extra repeating elements in the original array, the original array does not change $array=array_unique ($ ARRAY1);//Break the original array key values and re-order the value of the Alpha code from small to largeIndexThe $int=sort ($array) is recalculated starting with the number 0;//and sort in reverse, with the element value Ascol the size of the code value in descending order,IndexRe-$int=rsort from 0 ($array);//Returns an array of each element value in the first parameter array, in turn, to the parameter two array, the length of the two arrays must be the same, 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, 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-formatted string to an object or an array, the return type is determined by the second parameter, the second parameter defaults to False, In the JSON format string, the key and the value need to be enclosed in double quotation marks//If the return type is an object, after the first castis an array of elements of a key value in the array, the array is still coerced before using the array corresponding to that key value. 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-and property to access $decode_arr = Json_decode ($encode _str); Var_dump ($decode _arr->k1);
If the second argument is true then the array type is returned automatically, 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 format described, but the encoded string is not as readable as json_encode () encoded $STR = serialize ($arr); Var_dump ($STR);//With Json_ Decode () functions Similarly, converts a serialize () formatted string back to a variable before the format, and does not pass the second parameter to convert the original mode back to $arr = Unserialize ($STR); Var_dump ($arr);? >

The above is introduced (to) PHP commonly used functions, including pointer movement, require, index content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.