Summary of string and array common functions

Source: Internet
Author: User
Tags explode random seed truncated

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>

<body>
<?php
A review of string manipulation functions in PHP

Queries for a string that occurs in another string

$strA = "ESDNXZLKCSNKNLKFNJSERNVSZLKDFNGFLKDNLDSNALSDNAKJDNBKJBVAKKCS";

$strB = ' DN ';

$STC =strrpos ($strA, $strB); Strpos can be followed by three parameters, where the last parameter is optional and used to indicate the starting position of the calculation:

echo $STC. " <br/> ";

The Intercept function of the string and returns the truncated string

$strc =substr ($strA, 5,5); The function represents the punch of the fifth character to start the interception of $stra, the length of the Intercept is 5

echo $strc. " <br/> ";

$strc =strstr ($strA, "Kcs", true); The string is truncated from the string $stra, the starting position is "KCS", and the third parameter is an optional parameter that represents the forward intercept and the reverse intercept. The function itself is case-sensitive, preferably I if not qualified;

Substitution functions for strings

$strc =str_replace ("KCS", "AAA", $strA); A case-sensitive substitution that can be used to remove spaces that appear in a string

$strc =str_ireplace ("KSC", "AAA", $strA); case-insensitive substitution;

$strc =strtr ($strA, "KSC", "BBB"); The method of use is Str_replace, the only difference is the BBB here parameters can not be empty, where the replacement is completely replaced

Echo $strc;

Comparison functions for strings

$strD = strcmp ($strA, $strB); Used to compare the size of two strings, if two strings are equal to 0, if it is greater then Wei 1, or less then the return is-1, the comparison is only the first character

Echo $strD;

The hype that supersedes the whitespace at both ends of the string

Trim ();

A function that inserts a space in a string, or another character

Chunk_split ();

manipulating strings into arrays

Str_split ();//Cut according to the length of the character

Split ();//cut by character

Explode ();//cut by character

Common functions for arrays

Array_values (); Extracts all the key values in an array

Array_keys (); Extracts all the key names in the array

Array_flip (); The function is to exchange the key names and key values in the array, and if duplicates occur, they will be replaced by the following.

In_array ("Search", $strA); The value of the key to look up in the array is determined, and returns 1 if it exists, otherwise returns 0;

Array_search (); Used to search the array for the existence of the key value you want to query, if present, put back the corresponding key name;

Array_key_exists ();//used to find whether a key name exists in an array;

Isset ($arr [' a ']); Determine if the key name is set

pointer functions for arrays

Current ();//returns the key value of the object that the array is currently pointing to

Key (); Returns the key name referred to by the current pointer of the array

POS (); same function and current function

Prev (); Jumps the pointer of an array back to a

Next (); The pointer points to the next

each (); Converts the element corresponding to the current pointer to a new array and points the pointer to the next

End (); The pointer points to the last one;

Reset ();//pointer pointing to first bit

List ($key, $value) =each ($arr); Gets the key name and value of the current element of the array

Conversions between arrays and variables

Extract (); This function is very good, is the array to the key name variable, the key value of the variable corresponding to the value of a series of variables;

Compact (); Turns the given variable into an array

The reciprocal conversion between an array and a string

Explode ();//The function is a field split into a string

Implode ();//The method is to convert the array into characters

padding and splitting of arrays

Fragment of an array

Array_slice ($strA, 0, 3); An array is removed from the $stra, and the function ignores the key name;

Array_slice ($strA, 0,3,array (' Black ', "maroon"));//To remove a paragraph from the array, unlike the previous function, the returned sequence will be removed from the original array

Array_chunk ($strA, 2,true);//The function means dividing the specified array into two blocks and preserving the original key name. If you do not want to keep it, use false

Padding of arrays

Array_pad ($strA, 5, "X");//If the length of the array is not 5 bits, the remainder of the value will be populated with X

Arrays and stacks

Array_push (); Add one or more members at the end of the array

Array_pop (); Deletes a member at the end of an array

Array_shift (); Delete a delete in the head

Array_unshift (); Inserts a member into the array header

callback function

Array_walk ($arr, ' function ', ' words ');//Use a user function to comb through each idiom in the array (the third argument is passed to the callback function)

Array_mpa (' function ', $arr 1, $arr 2);//can handle multiple arrays (they should be the same length when two or more arrays are used);

Array_filter ($arr, "function");//Use a callback function to filter each element in the array, and if the callback function is true, the current element of the array is included in the returned result array, and the key name of the array remains unchanged. "Powerful", equivalent to each member of the array is filtered;

Array_reduce ($arr, "function", "*"); Convert to a single-valued function (* The first value of an array)


Sorting in the array

Sort ($arr);//Sort by order of small to large (the second parameter is sorted by what sort) ignore the array ordering of the key names

Rsort ($arr); Sort by large-to-small order (the second parameter is sorted by what sort) ignores the array of key names

Usort ($arr, "function"); Use user-defined comparison rows to sort the values in the array (function has two parameters, 0 equals, integer indicates the first is greater than the second, negative number indicates the first is less than the second), and the array ordering of the key names is ignored

Asort ($arr); Sort by order of small to large (the second parameter is sorted by what sort) the array of reserved key names

Arsort ($arr);//y sorted by the order of large to small (the second parameter is sorted by what way) the array of reserved key names

Uasort ($arr, "function"); Sorts the values in the array using the user's custom comparison function (there are two parameter 0 in function that represent equality, positive numbers indicate that the first is greater than the second, negative numbers indicate that the first one is less than the second) the array ordering of the reserved key names

Sort arrays by key names

Ksort ($arr); Sort by key name in a positive order

Krsort ($arr); Sort by key name in reverse order

Uksort ($arr, "function"); Use a user-defined comparison function to sort the key names in the array (there are two parameters in function, 0 for equality, positive for the first greater than the second, and negative for the first less than the second one)

Sort by natural sort

Natsort ($arr); Natural sort (ignores key names)

Natcasesort ($arr); Natural sort (ignores case, ignores key name)


Calculation of arrays

Array_sum ($arr); Sum operations of all elements inside an array

The collection of arrays

Array_merge ($arr 1, $arr 2); Two or more arrays of the same string key name, followed by overwriting the previous, the same number key name, the following will not do overwrite operation, but attached to the back)


The difference set of an array

Array_diff ($arr 1, $arr 2); Returns an array of difference results

ARRAY_DIFF_ASSOC ($arr 1, $arr 2);//returns the array of difference results, and the key names are also compared

Intersection of arrays

Array_intersect (); Returns an array of intersection results

Array_intersect_assoc (); Returns the intersection result array, the key name also makes the comparison;

The other array functions

Range (0,12);//Create an array containing the specified range of cells

Array_unique (); Remove duplicate values, the new array retains the original key name

Array_reverse ();//Returns an array whose cell order is the opposite of the original array, if the second argument is true to preserve the original key name
Srand (float) microtime () *10000000); Random seed Trigger

Array_rand ($arr, 2); Randomly fetching one or more elements from an array

Shuffle ($arr); Disturb the order of the arrays

?>
</body>

Summary of string and array common 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.