PHP Regular Expressions and arrays

Source: Internet
Author: User

<?php
Regular expressions
Slash stands for delimiter/^$/
/*
$str = "abcde123fg456h";
$reg = "/\d/";
Echo preg_replace ($reg, "#", $str);//replace the string str with the regular expression Reg #
Var_dump (Preg_split ($reg, $str)); Splitting a string str by regular expression

$arr =array ();
Preg_match ($reg, $str, $arr);//Match 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);
*/




Array
Defined
/*
$arr =array (1,2,3,4,5);//1. Defining an array 1
2. Assignment definitions
$arr [1]=5;
$arr [2]=6;
Var_dump ($arr);
*/

Array classification
1. Indexed arrays
/* $arr =array (1,2,3,4,5);
echo $arr [0];*/
2. Associative arrays
/* $arr =array ("code" =>5, "name" =>10);
echo $arr ["Name"];*/
PHP Array Features: Can store any type of data, the length can vary

$arr =array ("AA", "BB", 5, "CC", 10);
$arr =array ("A" =>10, "B" =>20, "c" = "Hello", "D" >=30);//associative arrays cannot be traversed with a for loop
For loop traversal
/*for ($i =0; $i <count ($arr); $i + +)
{
echo $arr [$i]. " <br> ";
}*/

2.foreach traversal
/* $arr =array ("A" =>10, "B" =>20, "c" = "Hello", "D" =>30);//associative arrays cannot be traversed with a for loop
foreach ($arr as $value)
{
echo $value. " <br> ";
}

foreach ($arr as $key = $value)
{
echo $key. " --". $value." <br> ";
}*/

3.each () function
/*
$arr =array ("A" =>10, "B" =>20, "c" = "Hello", "D" =>30);
Var_dump (each ($arr));//returns the key and value of the element inside the array
Var_dump (each ($arr));//Take the value down one at a time to point to the next value

4.list () function
$arr =array ("AA", "BB", 5, "CC", 10);
List ($a, $b, $c, $d) = $arr;//assigns the value of the array arr to a variable in the list function; list can be written less, not more
Echo $d;
*/

5.each () and list () iterate through the array.
/*
$arr =array ("AA", "BB", 5, "CC", 10);
while (list ($key, $value) =each ($arr))
{
echo "{$key}--{$value}<br>";
}*/

6. Pointers

$arr =array ("AA", "BB", 5, "CC", 10);
Var_dump (current ($arr)); Value that points to the position of the current pointer
echo Key ($arr);//Take the current pointer to the position of the key value
Next ($arr); Adjust the pointer to
echo Key ($arr);
Prev ($arr); Raise the pointer to
End ($arr); Move the pointer to the last
Rest ($arr); Reset the pointer to the first


$arr =array ("AA", "BB", 5, "CC", 10);
/*while (Next ($arr))
{
echo Key ($arr). " --". Current ($arr)." <br> ";
}*/


/*do
{
echo Key ($arr). " --". Current ($arr)." <br> ";
}
while (Next ($arr)) */

/*
$arr =array ("AA", "BB", 5,5, "CC", 10);
Var_dump (In_array ("AB", $arr));//Determine if there is a value in the array
Var_dump (Array_reverse ($arr));//Flip Array
echo count ($arr); Number of arrays
$attr =array_unique ($arr); Removes duplicates within the array
unset ($attr [0]); Delete an array
Var_dump (Array_values ($attr));//re-index

$attr =array (1,2,3,4,5);
Array_merge ($arr, $attr);//merge arrays
Array_push ($arr, "DD"); Add an element to an array
Var_dump ($arr);
*/


Two-dimensional arrays
/*
$attr =array (
Array (+/-),
Array (4,5,6),
Array (7,8,9)
);
Var_dump ($ATTR);
*/

Example
$attr =array (
Array (' n001 ', ' Han '),
Array (' n002 ', ' Miao '),
Array (' n003 ', ' hui '),
Array (' n004 ', ' Uighur '),
);
Displays the contents of the array in the drop-down list
echo "<select>";
foreach ($attr as $value)
{
echo "<option value= ' {$value [0]} ' >{$value [1]}</option>";
}
echo "</select>";

Display the contents of the array in a table
$attr =array (
Array (' P001 ', ' Zhang San ', ' Male ', ' Han '),
Array (' P002 ', ' John Doe ', ' female ', ' hui '),
Array (' P003 ', ' Harry ', ' Male ', ' Miao '),
Array (' p004 ', ' Zhao Liu ', ' female ', ' Uighur '),
);
echo "<table width= ' 800px ' border= ' 1px ' cellspacing= ' 0 ' cellpadding= ' 0 ' align= ' center ' >";
echo "<tr><td> number </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>";

PHP Regular Expressions and arrays

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.