First, regular expression
1. "/" stands for delimiters, "^" stands for the starting symbol, "$" for the end symbol
$str 1= "Abc123def45ghjk6789lou";
$str 2= "ABCDE5FG2H6JKL";
$reg = "/\d/"; Match a number
2. Echo preg_replace ($reg, "#", $str 1); Replace the string str1 with the regular expression Reg #
Output: abc## #def # #ghjk## # # # # ##lou
3. Var_dump (preg_split ($reg, $str 2)); The string str2 is split according to the regular expression Reg, returning an array
Output: Array (4) {[0]=> string (5) "ABCDE" [1]=> string (2) "FG" [2]=> string (1) "H" [3]=> string (3) "JKL"}
4. $str = "Pick Me up Oh Alice 18953364666 Crazy Group of calligraphy 13,853,369,999 poor odd ah";
$reg = "/(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8| 9]|170[0|5|7|8| 9]|17[6|7|8]|18[0-9]) \d{8}/"; Mobile phone number Regular expression
$arr =array ();
Preg_match ($reg, $str, $arr); Matches the first data in a string that satisfies a regular expression
Var_dump ($arr);
Preg_match_all ($reg, $str, $arr); // match all data in a string that satisfies a regular expression
Var_dump ($arr);
Second, array
1. Define the array:
Mode one: Standard array definition
$arr =array (1,2,3,4,5);
Method Two: Assignment definition:
$arr [0]=5;
$arr [1]=6;
Var_dump ($arr);
2. Classification:
2.1 Index Array: Writes only value values in an array, indexes are automatically generated, and values are based on indexed numbers
$arr =array (1,2,3,4,5);
echo $arr [0];
2.2 Associative array: key = Value,key corresponds to value one by one, paired existence
$arr = ("one"= 5, "One"and "10");
Echo $arr ["both"];
Note: Associative array relationships are clearer, key and value must be written in full
PHP Array Features: Can store any type of data, the length can vary, special about the associated array
3. Iterating through an array
3.1 for loop traversal: cannot traverse associative array
$arr =array ("AA", "BB", "CC", 5, "D", 10);
$arr =array ("A" =>10, "B" =>5, "c" = "Hello", "D" =>20);
for ($i =0; $i <count ($arr); $i + +)
{
echo $arr [$i]. " <br> ";
}
3.2 foreach traversal: can traverse associative arrays
Take value only:
foreach ($arr as $value)
{
echo $value. " <br> ";
}
Take key and value:
foreach ($arr as $key = $value)
{
echo $key. " --". $value." <br> ";
}
3.3 each () function
Var_dump (each ($arr)); Returns the key and value of the element inside the array
Var_dump (each ($arr)); Go down one at a time, point to the next value
Equivalent to a pointer, first time value, pointer to first element, second fetch, pointer to second element
3.4 list () function is special and appears on the left side of the equals sign
List ($a, $b, $c) = $arr; Assign the values inside the array to the variables in the list function, but the variables can be less, but not more
echo $a; Output: AA
Each () and list () are combined to iterate through the array: can traverse associative arrays
while (list ($key, $value) =each ($arr))
{
echo "{$key}--{$value}<br>";
}
3.5 pointer traversal: can traverse associative array
Var_dump (current($arr)); Value that points to the position of the current pointer
echo Key ($arr); Key that points to the position of the current pointer
Next ($arr); //Adjust the pointer to
echo $key ($arr);
prev ($arr); //Raise the pointer upward
End ($arr); //Adjust the pointer to the last
Reset ($arr); Resets the pointer, pointing to the first
do{
echo Key ($arr). " --". Current ($arr)." <br> ";
}
while (Next ($arr))
while (Next ($arr))
do{
echo Key ($arr). " --". Current ($arr)." <br> ";
}
Next () is executed first, and the first element is not taken.
4. Pre-defined arrays
Global//declared as Globals
$GLOBALS//referencing global variables available in global scope
Var_dump ($_server); Server and execution Environment information
Var_dump ($_ENV); Environment variables
Var_dump ($_get); HTTP get variable stores the value that the page passes over
Var_dump ($_post); The HTTP post variable stores the value that the page passes over
$_request//http REQUEST Variable
$_files//http FILES variables
$_session//session Variable
$_cookie//http Cookies
5. Common functions of arrays
$arr =array ("AA", "BB", "CC", 5, "D", 10);
$arr =array ("A" =>10, "B" =>5, "c" = "Hello", "D" =>20);
Var_dump (in_array ("AA", $arr)); Determines if there is a value in the array, returns true if it exists, or false to determine the value in the associative array, and cannot determine the key
Var_dump (array_reverse ($arr)); Flipping an array
Echo count ($arr); Returns the number of array elements
$attr =Array_unique ($arr); To return a new array, and if it is an indexed array, the index of the repeating element is also removed
unset ($arr [0]); | |unset ($arr ["a"]); Deletes an element in an array, depending on the index or key
Var_dump (array_values ($attr)); Re-index, key in associative array also becomes index
$attr = (1,2,3,4);
Var_dump (array_merge ($arr, $attr)); Merge arrays, re-index, associative array key invariant
Array_push ($arr, "DD"); Appends an element to an array to become the last element of the array
6. Two-dimensional array
$attr =array (
Array (+/-),
Array (4,5,6),
Array (7,8,9)
);
Var_dump ($ATTR);
Example 1:
$attr =array (
Array ("n001", "Han"),
Array ("n002", "Miao"),
Array ("n003", "Hui"),
Array ("n004", "Uighur")
);
1.1 Displays the contents of the two-dimensional array in the form of a drop-down list:
echo "<select>";
foreach ($attr as $value)
{
echo "<option value= ' {$value [0]} ' >{$value [1]}</option>";
}
echo "</select>";
1.2 Displays the contents of the two-dimensional array as a div:
<style>
. minzu{width=100px; background-color:red; color:white; border:1px solid;}
</style>
<body>
<?php
foreach ($attr as $v)
{
echo "<div class= ' Minzu ' >{$v [0]},{$v [1]}</div>";
}
?>
Example 2:
$attr = Array (
Array (' P001 ', ' Zhang San ', ' Male ', ' Han '),
Array (' P002 ', ' John Doe ', ' female ', ' hui '),
Array (' P003 ', ' Harry ', ' Male ', ' Han '),
Array (' p004 ', ' Zhao Liu ', ' female ', ' Miao ')
);
2.1 Displaying the contents of a two-dimensional array in a tabular format
echo "<table width= ' border= ' 1 ' cellpadding= ' 0 ' cellspacing= ' 0 ' >";
echo "<tr><td> code </td><td> name </td><td> Gender </td><td> National </td> </tr> ";
for ($i =0; $i <count ($attr); $i + +)
{
echo "<tr><td>{$attr [$i][0]}</td><td>{$attr [$i][1]}</td><td>{$attr [$i][2]} </td><td>{$attr [$i][3]}</td></tr>];
}
echo "</table>";
2.2 or reference the PHP code in the table:
<table width= ' border= ' 1 ' cellpadding= ' 0 ' cellspacing= ' 0 ' >
<tr><td> Code </td><td> name </td><td> Gender </td><td> National </TD></TR >
<?php
for ($i =0; $i <count ($attr); $i + +)
{
echo "<tr>
<td>{$attr [$i][0]}</td>
<td>{$attr [$i][1]}</td>
<td>{$attr [$i][2]}</td>
<td>{$attr [$i][3]}</td>
</tr> ";
}
?>
</table>
PHP Regular Expressions and arrays