Php array-related processing functions and str string processing and regular expressions

Source: Internet
Author: User
Tags comparison explode html tags modifier trim

Array-related processing functions:

1) array key-value operation functions
 
Array_values (); // obtain the value in the array
Array_keys (); // Obtain the keys in the array
In_array (); // check whether a value is in the array
Array_key_exists (); // check whether a key is in the array
Array_flip (); // key and value pairs
Array_reverse (); // reverse the value in the array

2) calculate the element and uniqueness of the array.

Count (); // count the number of arrays
Array_count_values (); // number of occurrences of the median in a statistical group
Array_unique (); // delete duplicate values in the array

3) use the callback function to process array functions
 
Array_filter (); // array value filtering
Array_map (); // calls the callback function to the unit of the given array.

4) sorting functions of arrays

 
Sort ();
// Sort the array values by value, in ascending order, without retaining the key
Rsort ();
// Sort the array values by value. The values are sorted in descending order and keys are not retained.
 
Asort ();
// Sort the array values by value, sort them in ascending order, and retain keys
Arsort ();
// Sort the array values by value, sort them in descending order, and retain keys
 
Ksort ();
// Sort the array values by pressing the key in ascending order and retain the key
Krsort ();
// Sort the array values by pressing the keys in descending order and retain the keys.
 
Natsort ();
// Sort by natural number
Natcasesort ();
// Ignore case-insensitive natural numbers for sorting
 
Array_multisort (); // sort another array with an array
SORT_DESC reverse order
SORT_ASC positive Order array_multisort ($ arr2, SORT_DESC, $ arr );

5) split, merge, decompose and combine functions

 
Array_slice (); // capture a segment of value $ arr2 = array_slice ($ arr,); 0 start position, 3, return value: intercepted content
 
Array_splice (); // extract a value and retain a value
 
Returned value: the remaining content after the truncation // array_splice (3, 3, "aa"); 3 values are truncated from the position of subscript 3, and aa replaces the intercepted content.
 
Array_combine (); // merge. One is the key, and the other is the value eg: $ arr3 = array_combine ($ arr1, $ arr2); $ arr1 is the key value, and $ arr2 is the value.
 
Array_merge (); Union set // merge, with the same key value. A new array $ arr3 = array_merge ($ arr1, $ arr2) is returned before overwriting );
 
Array_intersect (); // intersection
 
Array_diff (); // difference set
 
Implode (); // concatenate the array into a string such as: $ str = implode ("|", $ arr); | delimiter
  
Explode (); // splits the string into an array eg: $ arr = explode ('|', $ str );


6) array and data structure

 
Array_pop (); // The Last pop-up value, returns the pop-up value // unset ($ arr [count ($ arr)-1]);
Array_push (); array_push ($ arr, 6); // add a value from the end and return the number of arrays.
// $ Arr [] = "aa ";
Array_shift (); // A value is popped up from the front, and the removal value is returned. The original array subscript is rearranged.
// Unset ($ arr [0]); original array subscript is not rearranged
Array_unshift (); // insert a value from the front to return the number of arrays.

7) other useful array processing functions

 
Array_rand (); // random key
 
Shuffle (); // disrupt the array
 
Array_sum (); // sum of all values in the array
 
Range (); // Obtain an array in the specified range.
// Range (); returns the array (, 3,..., 10 );
// Range (, 2); array (, 9); 2 indicates the difference value; the default value is 0.

String Processing and regular expressions
----------------

1. Introduction to string processing
2. Common string output functions
3. Common string formatting functions
4. String comparison functions
5. Application of regular expressions in strings
6. perl-compatible regular expression functions

String output:

 
1) echo "hello world www.111cn.net ";
2) print "aaaa ";
3) die ("output an error message ");
4) printf ("-- % s ---- % s --", $ a, $ B );
% S string
% D number
% F floating point // %. 2f two digits after the decimal point
5) sprintf ("$ s % s", $ a, $ B );
Instead of directly outputting, the returned value is a new variable.

Common string formatting functions:
1. Remove spaces and string filling functions
 
Ltrim () // remove the left space
Rtrim () // remove the right space;
Trim () // remove the two spaces $ str = 'abc'; trim ($ str, 'B'); echo $ str; result: ac; the specified string can be deleted.
Str_pad () // add a space or string to the string
 
<? Php
$ Input = "Alien ";
Echo str_pad ($ input, 10); // output "Alien"
Echo str_pad ($ input, 10, "-=", STR_PAD_LEFT); // output "-=-Alien"
Echo str_pad ($ input, 10, "_", STR_PAD_BOTH); // output "_ Alien ___"
Echo str_pad ($ input, 6, "___"); // output "Alien _"
?>

2. String case conversion function

 
Strtolower ()
Strtoupper ()
Ucfirst ()
Ucwords ()

3. String functions associated with html tags

 
Nl2br ()
Htmlspecialchars ()
Strip_tags ()
Addslashes ()
Stripslashes ()

4. Other string formatting functions

 
Strrev ()
Strlen () // returns the length of the string
Number_format ()
Md5 () // unidirectional irreversible encryption
Str_shuffle () // random output string

String comparison functions:
1. Compare strings by byte
1
 
Strcmp ($ str1, $ str2) // compare each byte of a string
 
Strcasecmp () // ignore each byte of the case-sensitive comparison string

2. String comparison by natural sorting

 
Strnatcmp ();
// Compare the numbers in a string in natural order

String segmentation and concatenation:
1. Segmentation
 
// Split the string into an array
Explode ()
Preg_split ('//', $ str );

2. Splicing
// Concatenates an array into a string

 
Implode ()
Join () // equal to implode ();

String truncation:

 
Substr ()

String search:
 
Strstr () // finds the first occurrence of a specified character in a string
 
 
Strrchr () // find the last occurrence of a specified character in the string
 
 
Strpos () // the position where w first appears in $ str
Strrpos ($ str, 'w') // The last position of w in $ str

String replacement:
1
 
Str_replace ()

Supports multi-byte text

 
Mb_substr ($ str, "UTF-8 ");
Mb_strpos ();
Mb_strrpos ();
Mb_strstr ();
Mb_strtoupper ();
Mb_strtolower ();

Application of regular expressions in strings:
I. INTRODUCTION to regular expressions
A regular expression is a syntax rule used to describe character arrangement and matching modes. It is mainly used for character string mode segmentation, matching, search and replacement operations.

A style is generally a procedural description of a text pattern consisting of regular characters and special characters. Here we use perl compatible regular expressions.
II. Regular expression syntax
1. Atom www.111cn.net
1) single character, number
Any character in a-z, A-Z, 0-9 a-z
2) mode unit
(Abc) matches abc and forms a unit.
3) atomic table
[Abc] any one of the characters a, B, or c
4) re-use mode unit
\ 1, $1
5) common escape characters
D, D, w, W, s, S
D. Match a number.
D. Match a non-number.
W matching letters, numbers, and underscores
W in addition to letters, numbers, and underscores
S matches blank characters, spaces, tab
In addition to blank characters, spaces, and tabs
6) convert to metacharacters
*,. []
2. Metacharacters
*, + ,?, |, ^, $, B, B, [], [^], {m}, {m, n}, {m ,},(),.
D * one or more or zero numbers
D + one or more numbers
D? One or zero digits
. Any character
3. Pattern modifier
I, m, s, U, e
I case-insensitive
M is considered as multi-row
S is regarded as a row
U greedy mode, Max mode
E is used for replacement. You can use function processing to reference \ 1, $1.
III. String regular expression functions
1. String matching and searching
Preg_match ()
Preg_match_all ()
Preg_grep ()
2. String replacement
Preg_replace ()
// Problem: regular e modifier <>
3. String segmentation and connection
Preg_split ()
4. web verification application of regular expressions
1) email address
2) url
3) telephone number
 
 
Ubb editor:
 
[Url] [/url] [B] Text [/B]

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.