Regular expressions
1. Replace
$s = "Hello5world"; $s Preg_replace ("/\d/", "#",$s); Echo $s ; // The output is Hello#world, and the number is replaced by #. The first number is the replaced content, and "/\d/" represents the number of the regular expression. The second parameter is what you want to replace. The third parameter is the array to manipulate.
2. Splitting
//$s = "hello5world",//$attr = Preg_split ("/\d/", $s),//Split//var_dump ($arr) According to regular expression,/////The first parameter is split by what, The second argument is which string to split.
3. Match
// $s = "he8llo5wor6ld";//$arr = Array ();//preg_match ("/\d/", $s, $arr);//matches the first string that satisfies the regular, where the match is 8 (8) is the first number in the string. Preg_match_all ("/\d/", $s, $arr); Matches all occurrences of a regular string, matching all numbers (8, 5, 6). The first parameter is what matches the//second parameter is the matching array//The third parameter is another array, or it can be an empty array, and the array after matching is placed in this array.
Array
1. Iterating through an array
(1) For loop traversal, can only traverse indexed array
//$attr = Array ("AA", "BB", "CC", "DD");
/* For ($i = 0; $i <count ($attr); $i + +)
{echo $attr [$i]. " <br> "; } */
(2) foreach traversal, associations, and indexes can be traversed.
//$attr = Array ("AA", "BB", "CC", "DD");/*foreach ($attr as $v) {echo $v. <br> ";}*///only the value can be traversed/*foreach ($attr as $k = + $v) {echo $k. --". $v." <br> ";}*///can traverse out key and value$attr=Array(1,2,3,4,5);foreach($attr as $k=$v) {if($v= = 3) {unset($attr[$k]);//Delete an element or delete a variable, not directly unset ($v), because the time elapsed is to copy the number 3 inside the array to $v, delete is copied, not the inside of the array. }}$attr=array_values($attr);//re-index, after removing 3 of the array element, the index value becomes 0,1,3,4. Re-index becomes 0,1,2,3. Var_dump($attr);//In other languages, when you use foreach to iterate through the elements inside an array, you cannot delete or add elements inside the foreach. Only PHP can be inside.
Pre-defined arrays
1. $GLOBALS referencing global variables in the global scope
Var_dump$GLOBALS ()//$GLOBALS is an array that is system-customized, cannot be modified, and must be capitalized. Represents an array of global variables.
2.$_server is the server information
3.$_ENV represents environment variables
4.$_get represents the data that the user passes in the _get way
5.$_post represents the data that the user transmits by post
Method and the way to obtain the corresponding. Var_dump ($_post)
6.$_files; is about the form submission file.
<form action= "text.php" method= "post" enctype= "Multipart/form-date" >// Submit file with Enctype attribute required, submit by post
7.$_session
Session control, the mechanism for saving user login information. If you log on to the previous page for a certain period of time no action, refresh the page will require re-login, this is the effect of _session. It has a fixed time after which the login information is cleared. The _session store login information is stored on the server. The security of the _session will be better.
8.$_cookie
Session control, the mechanism for saving user login information. If you log on to the previous page for a certain period of time no action, refresh the page do not need to re-login, this is the effect of _cookie. The _cookie store login information is stored on the hard disk. _cookie is less secure than _session.
Other common functions 1.in_array(, ); Determine if a value is present in the array, there are two parameters in the parentheses, the first parameter is the value of whether it exists, and the second argument is an array of checks. 2. Array_reverse (); Flip the function, and write the array to be flipped in parentheses. 3. Array_unique (); Remove duplicate values, go to heavy. 4. unset ($attr[0]); Deletes a value in the array. 5. array_values (); re-index. Commonly used with deletions. 6. Array_merge (); Merge the array, and fill in the parentheses with n the array to be merged. 7. Array_push (); Add a value to the array and add the position at the end.
Array examples
1. Take the contents of the array to make a drop-down list
method One: (Embed PHP code)$attr=Array(//defines an array, using a two-dimensional array. Array("n001", "Han"),Array("n002", "Hui"),Array("n003", "Manchu"),Array("n004", "Uighur"));<select><option> all </option><?PHP/*foreach ($attr as $v) {echo "<option>{$v [1]}</option>";}*/?></select>method Two: (No need to embed PHP code)$attr=Array(//defines an array, using a two-dimensional array. Array("n001", "Han"),Array("n002", "Hui"),Array("n003", "Manchu"),Array("n004", "Uighur"));Echo"<select>";Echo"<option> All </option>";foreach($attr as $v) {Echo"<option>{$v[1]} </option> ";}Echo"</select>";
2. Two-dimensional array traversal to table
$attr=Array( Array("Code", "Name", "Nation", "age"),Array("001", "Zhang San", "Han", "23"),Array("002", "John Doe", "Hui", "34"),Array("003", "Harry", "Uighur", "53"),Array("004", "Song Liu", "Zhuang", "26"));Echo"<table border= ' 1px ' >"; for($i= 0;$i<Count($attr);$i++){ Echo"<tr>"; for($j= 0;$j<Count($attr[$i]);$j++) { Echo"<td>".$attr[$i][$j]." </td> "; } Echo"</tr>";}Echo"</table>";
PHP Arrays--array regular expressions, arrays, predefined arrays