PHP Tutorial (array)1. 1
Using Array
array คือชุดข้อมูลที่ถูกนำำมำเรียงต่อๆ กันอย่ำงมี
ลำำดับ โดยเก็บ
ในตัวแปร ชนิด array สำมำรถอ้ำงถึงสมำชิก
แต่ละตัว (element)
ด้วย index
$ar_ID[0] = 13900990;
$ar_ID[1] = 13901032;
2. 2
Using Array
ฟังก์ชั่น foreach() ใช้ดึงข้อมูลโดยไม่อ้ำงถึง index ของ
ตัวแปร
array ออกมำทำำงำนเป็นรอบ โดยไม่ใช้ loop
รูปแบบที่ 1
foreach(array_expression as $value){
statement;
}
รูปแบบที่ 2
foreach(array_expression as $key => $value){
statement;
}
3. 3
Using Array
สร้ำง array ด้วยฟังก์ชั่น array()
Demo
<? #array01.php
$ar_name = array(“ทินกร",“มำนพ", “กวิน");
foreach($ar_name as $eachname)
{
echo $eachname.”<br>”;
}
?>
4. 4
Using Array
สร้ำง array ด้วยกำรกำำหนดค่ำให้กับตัวแปร array
Demo
<? #array02.php
$ar_ID[] = 13900990;
$ar_ID[] = 13901032;
$ar_ID[] = 13900854;
foreach($ar_ID as $i => $Emp_ID)
{
print(($i+1)."). ".$Emp_ID."<br>");
}
?>
5. 5
Using Array
สร้ำง array ด้วยกำรกำำหนดค่ำให้กับตัวแปร array
Demo
<? #array03.php
$country[] = "China";
$country[] = "Thailand";
$country[] = "India";
$country[] = "Japan";
print "<select name=selCountry>";
foreach($country as $countryCode => $countryName){
print("<option value=$countryCode>$countryName</option>");
//print $countryName."<br>";
}
print "</select>";
?>
6. 6
Using Array
สร้ำง array ด้วยกำรกำำหนดค่ำให้กับตัวแปร array
Demo
<? #array05.php
$country = array("CH" => "China",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
print "<select name=selCountry>";
foreach($country as $countryCode => $countryName){
print("<option value=$countryCode>$countryName</option>");
//print $countryName."<br>";
}
print "</select>";
?>
7. 7
Using Array
สร้ำง array ด้วยฟังก์ชั่น range()
ใช้ในกำรสร้ำง array ที่มีค่ำเป็นลำำดับจำำนวนเต็ม เช่น
array(1,2,3,4,5) หรือ เป็นลำำดับของตัวอักษรตัวเดียว เช่น
array(“a”, “b”, “c”, “d”, “e”)
Demo
<? #array04.php
$ar_char=range(“A",“Z");
foreach($ar_char as $ar_value){
print("- ".$ar_value."<br>");
}
?>
8. 8
Using Array
ฟังก์ชัน array_unique() ใช้กำำจัดข้อมูลใน
array ที่มีค่ำซำ้ำกัน
Demo
<? #arunique.php
$arFruit = array("apple","mango","guava","apple","strawberry");
$aruFruit = array_unique($arFruit);
print "Fruit in arFruit[] <br>";
foreach($arFruit as $index => $fruit){
print($index+1)." - $fruit<br>";
}
print "Fruit in aruFruit[] <br>";
foreach($aruFruit as $index => $fruit){
print($index+1)." - $fruit<br>";
}
?>
9. 9
Using Array
กำรเรียงลำำดับข้อมูลใน array ด้วยฟังก์ชัน
sort(), rsort()
เรียงลำำดับข้อมูลจำกน้อยไปหำมำก (ascending) แบบ case
sensitive
ใช้ sort()
เรียงลำำดับข้อมูลจำกมำกไปหำน้อย (descending) แบบ case
sensitive
ใช้ rsort()
Demo
<? #sort.php
$arFruit = array("apple","mango","guava","apple","strawberry");
$aruFruit = array_unique($arFruit);
sort($aruFruit);
print "Fruit in aruFruit[] - sort asc.<br>";
foreach($aruFruit as $index => $fruit){
print($index+1)." - $fruit<br>";
}
?>
10. 10
Using Array
กำรเรียงลำำดับข้อมูลใน array ด้วยฟังก์ชัน
asort(), arsort()
เรียงลำำดับข้อมูลจำกน้อยไปหำมำก (ascending) เหมือน
sort()
เรียงลำำดับข้อมูลจำกมำกไปหำน้อย (descending) เหมือน
rsort()
แต่ไม่จัดเรียงคีย์ใหม่
Demo
<? #asort.php
$arFruit = array("apple","mango","guava","apple","strawberry");
$aruFruit = array_unique($arFruit);
asort($aruFruit);
print "Fruit in aruFruit[] sort asc.<br>";
foreach($aruFruit as $index => $fruit){
print($index+1)." - $fruit<br>";
}
?>
11. 11
Using Array
กำรเรียงลำำดับข้อมูลใน array ด้วยฟังก์ชัน
natsort()
หำกข้อมูลที่ต้องกำรเรียงลำำดับหน้ำตำเหมือนกันแต่ลงท้ำยด้วยตัวเลขที่สำมำรถจัดลำำดับ
ได้จะพบปัญหำว่ำกำรเรียงลำำดับเป็นดังนี้ 1 10 2 20
natural sort หรือ natsort() สำมำรถแก้ปัญหำได้แบบ case sensitive
Demo
<?php #natsort.php
$array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png");
echo "Start datan";
print_r($array1);
print "<br>";
sort($array1);
echo "Standard sortingn";
print_r($array1);
print "<br>";
natsort($array2);
echo "nNatural order sortingn";
print_r($array2);
?>
12. 12
Using Array
กำรเรียงลำำดับข้อมูลใน array ด้วยฟังก์ชัน
natcasesort()
ช่วยในกำรเรียงลำำดับข้อมูลเหมือน natsort() แต่เป็นแบบ case insensitive
Demo
<?php #natcasesort.php
$array1 = $array2 = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');
echo "Start datan";
print_r($array1);
print "<br>";
sort($array1);
echo "Standard sortingn";
print_r($array1);
print "<br>";
natcasesort($array2);
echo "nNatural order sorting (case-insensitive)n";
print_r($array2);
?>
13. 13
Using Array
กำรเรียงลำำดับข้อมูลใน array ด้วยฟังก์ชัน
ksort(), krsort()
ช่วยในกำรเรียงลำำดับข้อมูลโดยคงควำมสัมพันธ์ระหว่ำงคีย์กับข้อมูล (ตำม
index)
Demo
<?php #ksort.php
$city = array("Thailand" => "Bangkok",
"Japan" => "Tokyo",
"Canada" => "Ottawa",
"China" => "Beijing",
"India" => "Dehli");
ksort($city);
foreach($city as $country => $capital){
print $country." - $capital<br>";
}
?>
14. 14
Using Array
กำรเรียงลำำดับข้อมูลใน array ด้วยฟังก์ชัน
array_reverse()
ช่วยในกำรเรียงลำำดับข้อมูลใน array ใหม่โดยค่ำ element สุดท้ำยจะขึ้น
มำเป็นค่ำแรก และค่ำแรก
กลับมำเป็นค่ำสุดท้ำย
Demo
<?php #arreverse.php
$city = array("Thailand" => "Bangkok",
"Japan" => "Tokyo",
"Canada" => "Ottawa",
"China" => "Beijing",
"India" => "Dehli");
$result = array_reverse($city);
foreach($result as $country => $capital){
print $country." - $capital<br>";
}
?>
15. 15
Using Array
กำรรวม array เข้ำด้วยกัน (array merging)
ใช้ operator + ในกำรรวม array 2 ชุด เข้ำด้วยกัน
หำกค่ำใน array ทั้งสองที่นำำมำรวมกันมี ” ”คีย์ ที่ซำ้ำ
กัน element
นั้นจะไม่ถูกรวมมำด้วย (element ของอำร์เรที่นำำ
มำรวมทีหลัง)
16. 16
Using Array
กำรรวม array เข้ำด้วยกัน (array merging)
ใช้ operator +
Demo
<? #operator.php
$oldcountry = array("CH" => "Chaina",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
$newcountry["CH"] = "Chiangmai";
$newcountry["TH"] = "Thibet";
$newcountry["HK"] = "Hong Kong";
$country = $oldcountry + $newcountry;
foreach($country as $countrycode => $countryname){
print $countrycode." - $countryname<br>";
}
?>
17. 17
Using Array
กำรรวม array เข้ำด้วยกัน (array merging)
กำรใช้ฟังก์ชั่น array_merge()
ใช้รวม array 2 ชุดเข้ำด้วยกัน หำกค่ำใน array ทั้งสองที่
นำำมำรวมกันมี “ ”คีย์ ซำ้ำกัน element นั้นจะถูกแทนที่ด้วยค่ำ
ของ element ที่มีคีย์ที่เหมือนกัน
18. 18
Using Array
กำรรวม array เข้ำด้วยกัน (array merging)
ใช้ฟังก์ชัน array_merge()
Demo
<? #armerge.php
$oldcountry = array("CH" => "Chaina",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
$newcountry["CH"] = "Chiangmai";
$newcountry["TH"] = "Thibet";
$newcountry["HK"] = "Hong Kong";
$country = array_merge($oldcountry , $newcountry);
foreach($country as $countrycode => $countryname){
print $countrycode." - $countryname<br>";
}
?>
19. 19
Using Array
การรวม array เข้าด้วยกัน (array merging)
การใช้ฟังก์ชั่น array_merge_recursive()
ใช้รวม array 2 ชุดเข้าด้วยกัน หากค่าใน array ทั้งสองที่
นำามารวมกันมี” ”คีย์ ซำ้า
กัน ค่าที่ซำ้ากันจะถูกนำามารวมกันเป็น array ที่ซ้อนอยู่ใน
array หลัก มีคีย์
เป็นเลขตั้งแต่ 0 ไปตามลำาดับ
20. 20
Using Array
การรวม array เข้าด้วยกัน (array merging)
ใช้ฟังก์ชัน array_merge_recursive()
Demo
<? #armrec.php
$oldcountry = array("CH" => "Chaina",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
$newcountry["CH"] = "Chiangmai";
$newcountry["TH"] = "Thibet";
$newcountry["HK"] = "Hong Kong";
$country = array_merge_recursive($oldcountry , $newcountry);
foreach($country as $countrycode => $countryname){
if(gettype($countryname) == "array"){
print "[$countrycode]:<br>";
foreach($countryname as $key => $cname){
print " [$key] - $cname<br>";
}
}else{
print "[$countrycode] : $countryname<br>";
}
}
?>
21. 21
Using Array
การหา element ที่แตกต่างกันระหว่าง array
ใช้ฟังก์ชั่น array_diff()
ใช้สำาหรับหา element ของ array ที่ระบุมาเป็น
argument ตัวแรก
ที่มีค่า “ ”คีย์ ไม่ซำ้ากับใน array ที่ระบุเป็น argument
ถัดๆ ไป
22. 22
Using Array
การหา element ที่แตกต่างกันระหว่าง array
ใช้ฟังก์ชัน array_diff()
Demo
<? #ardiff.php
$oldcountry = array("CH" => "Chaina",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
$newcountry["CH"] = "Chaina";
$newcountry["TH"] = "Thailand";
$newcountry["HK"] = "Hong Kong";
$country = array_diff($oldcountry , $newcountry);
foreach($country as $countrycode => $countryname){
print "[$countrycode] : $countryname<br>";
}
?>
23. 23
Using Array
การหา element ที่เหมือนกันระหว่าง array
ใช้ฟังก์ชั่น array_intersect()
ใช้สำาหรับหาค่าสมาชิกของ array ที่มีค่า “ ”คีย ซำ้ากัน
ระหว่าง array ตั้งแต่สองตัวขึ้นไป โดยผลลัพธ์ที่ได้ยังคง
คีย์เดิมเอาไว้
24. 24
Using Array
การหา element ที่แตกต่างกันระหว่าง array
ใช้ฟังก์ชัน array_intersect()
Demo
<? #arintersect.php
$oldcountry = array("CH" => "Chaina",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
$newcountry["CH"] = "Chaina";
$newcountry["TH"] = "Thailand";
$newcountry["HK"] = "Hong Kong";
$country = array_intersect($oldcountry , $newcountry);
foreach($country as $countrycode => $countryname){
print "[$countrycode] : $countryname<br>";
}
?>
25. 25
Using Array
การถอนค่า (pop) และการเพิ่มค่า (push) ใน
array
ใช้ฟังก์ชั่น array_pop(), array_push()
array_pop() ใช้สำาหรับหาค่า element สุดท้ายของ array
แล้วลบ
element สุดท้ายใน array นั้นทิ้ง โดยผลลัพธ์ที่ได้ยังคีย์เดิม
เอาไว้
รูปแบบ
mixed array_pop(array array)
erray_push() ใช้สำาหรับเพิ่มค่า element ใหม่เข้าไปใน
array
รูปแบบ
int array_push(array array, mixed var[, mixed.])
26. 26
Using Array
การถอนค่า (pop) และการเพิ่มค่า (push) ใน array
ใช้ฟังก์ชัน array_pop(), array_push()
Demo
<? #arpop.php
$country = array("CH" => "Chaina",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
$last = array_pop($country);
print_r($country);
print "<br>POP Item = $last";
?>
27. 27
Using Array
การถอนค่า (pop) และการเพิ่มค่า (push) ใน array
ใช้ฟังก์ชัน array_pop(), array_push()
Demo
<? #arpush.php
$country = array("CH" => "Chaina",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
$newpush = "USA";
$new = array_push($country, $newpush);
print_r($country);
print "<br>New PUSH Item = $new";
?>
28. 28
Using Array
การย้าย-ตัด-จัดเรียง-เลื่อน-แทรก-แทนที่ element ใน
array
ใช้ฟังก์ชั่น array_splice()
ในการใช้งาน array เราอาจมีความจำาเป็นต้องลบค่าบางค่า
ภายใน
array ออกโดยต้องการให้มีการจัดลำาดับใหม่ หรือต้องการ
แทรก
ค่าเข้าไปใน array ที่มีอยู่เดิม
รูปแบบ
array array_splice(array input, int offset [,int length
[,array replacement])
Input คืออาเรย์ที่จะถูกดำาเนินการ
Offset คือตัวเลขระบุตำาแหน่ง element เริ่มต้นของการดำาเนินการ
29. 29
Using Array
การย้าย-ตัด-จัดเรียง-เลื่อน-แทรก-แทนที่ element ใน array
ใช้ฟังก์ชัน array_splice()
การตัดบาง element ออกและจัดลำาดับคีย์ใหม่ด้วยฟังก์ชัน array_splice()
Demo
<? #arsplice.php
$country = array("CH" => "Chaina",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
print "Before splice...<br>";
foreach($country as $countrycode => $countryname){
print("$countrycode => $countryname<br>");
}
array_splice($country, 1, 2);
print "<br>After splice...<br>";
foreach($country as $countrycode => $countryname){
print("[$countrycode] => $countryname<br>");
}
?>
30. 30
Using Array การย้าย-ตัด-จัดเรียง-เลื่อน-แทรก-แทนที่ element ใน array
ใช้ฟังก์ชัน array_splice()
การแทนที่ค่าใน array ด้วยฟังก์ชัน array_splice()
Demo
<? #arsplice02.php
$country = array("CH" => "Chaina",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
$other = array("Hong Kong", "Korea");
print "Before splice...<br>";
foreach($country as $countrycode => $countryname){
print("$countrycode => $countryname<br>");
}
$remove = array_splice($country, 1, 2, $other);
print "<br>After splice...<br>";
foreach($country as $countrycode => $countryname){
print("[$countrycode] => $countryname<br>");
}
print "<br>Remove elements...<br>";
foreach($remove as $countrycode => $countryname){
print("[$countrycode] => $countryname<br>");
}
?>
31. 31
Using Array
การนับจำานวน element ใน array
ใช้ฟังก์ชั่น count()
ใช้นับจำานวน element ใน array
รูปแบบ
int count(mixed var [, int mode])
var คือตัวแปรหรือค่าอาเรย์ที่ต้องการนับจำานวน element
Demo
<? #count.php
$country = array("CH" => "Chaina",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
print count($country);
?>
32. 32
Using Array
การตัวชี้กลับไปที่ element แรกของ array
ใช้ฟังก์ชั่น reset()
ใช้เลื่อนตัวชี้กลับไปที่ element แรกของ array
Demo
<? #reset.php
$country = array("CH" => "Chaina",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
print count($country)."<br>";
print reset($country);
?>
33. 33
Using Array
การอ่านค่าใน array
ใช้ฟังก์ชั่น each()
จะให้ค่าเป็น คีย์ และ element ของ array ที่ตัวชี้ทำาการชี้
อยู่ใน
ขณะนั้นพร้อมทั้งจะเลื่อนตัวชี้ไป element ถัดไป
Demo
<? #each.php
$country = array("CH" => "Chaina",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
while($val = each($country)){
print $val[key]." => ".$val[value]."<br>";
}
?>
34. 34
Using Array
การนำาค่าใน array ใส่ลงในตัวแปร
ใช้ฟังก์ชั่น list()
เป็นวิธีการในการนำาค่าของแต่ละ element ของ
array มาแยก
ลงในตัวแปรแต่ละตัว เมื่อทราบจำานวน element
แน่นอน
Demo
<? #list.php
list($day, $month, $year) = explode("-",date("d-m-Y"));
print " $day = ".$day."<br>";
print " $month = ".$month."<br>";
print " $year = ".$year."<br>";
?>
35. 35
Using Array
การทำาลายตัวแปร array
ใช้ฟังก์ชั่น unset()
เป็นวิธีการยกเลิกตัวแปร array หรือเป็นการคืนหน่วยความจำาให้กับ
คอมพิวเตอร์
Demo
<? #unset.php
$country = array("CH" => "Chaina",
"TH" => "Thailand",
"IN" => "India",
"JP" => "Japan");
unset($country);
//print_r($country);
while(list($countrycode, $countryname) = each($country)){
print $countrycode." => ".$countryname."<br>";
}
?>
36. 36
Using Array
array 2 มิติ
ใน php ไม่มีข้อจำากัดเกี่ยวกับขนาดของมิติของ array ขึ้นอยู่กับหน่วยความจำา
Demo
<?#dim.php
$number = array(2 => array(4,6,8), 3 => array(6,9), 4 => array(8));
print "<table border=1>";
print "<tr>";
foreach($number as $parent => $child){
print "<td align='center' colspan='".count($number[$parent])."'>".$parent;
}
print "</td></tr><tr>";
foreach($number as $parent => $child){
foreach($child as $key => $value){
print "<td align='center'>".$value;
}
}
print "</td></tr></table>";
?>
37. 37
ฟังก์ชั่นเกี่ยวกับเวลา
date()
เป็นฟังก์ชันที่ใช้รับวันที่และเวลา ณ ปัจจุบันของเครื่อง
คอมพิวเตอร์ สามารถกำาหนดรูปแบบของวันที่และเวลาได้
รูปแบบ
String date (string format [, int timestamp])
format คือรูปแบบวันที่และเวลาซึ่งจะแทนด้วยตัวอักษรภาษา
อังกฤษ 1 ตัว
d แทนวันที่เลข 2 หลัก 01-31D แทนวันในสัปดาห์ Sun-
Sat
m แทนเดือนเลข 2 หลัก 01-12 M แทนชื่อย่อของเดือน
Jan-Dec
n แทนเดือนไม่มี 0 นำาหน้า 1-12
y แทนปี ค.ศ. เลข 2 หลัก 98,99,05 Y แทนปี ค.ศ. เลข 4
หลัก 1998
39. 39
ฟังก์ชั่นเกี่ยวกับเวลา
getdate()
เป็นฟังก์ชันที่ใช้รับวันที่และเวลา ณ ปัจจุบันของเครื่อง
คอมพิวเตอร์ โดยฟังก์ชันจะให้ผลลัพธ์ที่เป็น array ของวัน
เดือน ปี และเวลาในรูปแบบต่างๆ
รูปแบบ
array getdate([int timestamp])
timestamp เป็นตัวเลข integer ลำาดับเวลาทุกหนึ่งนาที
โดย getdate จะคืนค่ามาเป็น array จำานวน 10 elements
ได้แก่
second – วินาที minutes – นาที hours
– ชั่วโมง
mday – วันที่ wday – วันในสัปดาห์(0-6) mon – เลขเดือน
43. 43
ฟังก์ชั่นเกี่ยวกับเวลา
time(), mktime()
เป็นฟังก์ชั่นที่ใช้รับเวลา ณ ปัจจุบันของเครื่องคอมพิวเตอร์
Demo
<?#time.php
print "time ".time()." = ".date("H:i:s", time())."<br>";
print "mktime = ".mktime()." = " .date("H:i:s",
mktime())."<br>";
?
ทั้งสองฟังก์ชั่นต่างกันตรงที่ mktime() สามารถรับ argument
ตัวเลขวันที่ และเวลาเข้าไปเพื่อสร้าง timestamp ตามวันที่และ
เวลานั้นได้
รูปแบบ
Int mktime(int hour, int minute, int second, int mohth, int
day, int year[, int is_dst])
48. 48
Using Regular Expression
ใช้ตรวจสอบรูปแบบของข้อมูลที่ผู้ใช้ป้อนเข้า
เข้าระบบ เช่น หมายเลขโทรศัพท์, e-mail
address และ URL ของเว็บเพจ เป็นต้น
สัญลักษณ์ที่ใช้ในการสร้างรูปแบบ (not
complete)
สัญลักษณ์ ^
ใช้ตรวจสอบตัวอักษรหรือข้อความที่สอดคล้องกับรูป
แบบที่อยู่ข้างหน้าเครื่องหมาย ^ เช่น (ตรวจสอบรูป
แบบข้างหลังเครื่องหมาย)
“http^”
49. 49
Using Regular Expression
สัญลักษณ์ที่ใช้ในการสร้างรูปแบบ (complete)
สัญลักษณ์ $
ใช้ตรวจสอบตัวอักษรหรือข้อความสุดท้ายที่สอดคล้อง
กับรูปแบบที่อยู่หน้าเครื่องหมาย $ เช่น (ตรวจสอบรูป
แบบข้างหน้าสอดคล้องกับอักษรที่กำาหนด)
“com$”
“http://www.tot.co.th” จะให้ผลลัพธ์เป็นเท็จ
“http://www.google.com” จะให้ผลลัพธ์เป็นจริง
Regexp02.php
52. 52
Using Regular Expression
สัญลักษณ์ที่ใช้ในการสร้างรูปแบบ (not
complete)
สัญลักษณ์ ?
ใช้เปรียบเทียบส่วนใดส่วนหนึ่งของข้อความหาก
สอดคล้องกับตัวอักษรหรือรูปแบบที่อยู่หน้าเครื่องหมาย
คำาถามนี้จำานวนศูนย์หรือหนึ่งครั้งจะให้ผลลัพธ์เป็นจริง
เช่น
“t?”
“http://www.tot.co.th” จะให้ผลลัพธ์เป็นจริง
“http://www.cat.co.th” จะให้ผลลัพธ์เป็นเท็จ
“ot?.c”
53. 53
Using Regular Expression
สัญลักษณ์ที่ใช้ในการสร้างรูปแบบ
สัญลักษณ์ *
ใช้เปรียบเทียบส่วนใดส่วนหนึ่งของข้อความหาก
สอดคล้องกับตัวอักษรหรือรูปแบบที่อยู่หน้าเครื่อง
ดอกจันนี้จำานวนศูนย์หรือมากกว่าหนึ่งครั้งจะให้ผลลัพธ์
เป็นจริง เช่น
“to*t”
“http://www.tot.co.th” จะให้ผลลัพธ์เป็นจริง
“http://www.cat.co.th” จะให้ผลลัพธ์เป็นเท็จ
“http://www.tatoothai.com” จะให้ผลลัพธ์เป็นจริง
54. 54
Using Regular Expression
สัญลักษณ์ที่ใช้ในการสร้างรูปแบบ
สัญลักษณ์ +
ใช้เปรียบเทียบส่วนใดส่วนหนึ่งของข้อความที่
สอดคล้องกับตัวอักษรหรือรูปแบบที่อยู่หน้าเครื่องหมาย
นี้จำานวนหนึ่งครั้งหรือมากกว่า เช่น
“to+t”
“http://www.tot.co.th” จะให้ผลลัพธ์เป็นจริง
“http://www.cat.co.th” จะให้ผลลัพธ์เป็นเท็จ
“http://www.tatoothai.com” จะให้ผลลัพธ์เป็นจริง
56. 56
Using Regular Expression
สัญลักษณ์ที่ใช้ในการสร้างรูปแบบ
สัญลักษณ์ [ ]
ใช้แสดงกลุ่มตัวอักษรหรือกลุ่มคำาในการเปรียบเทียบ
เช่น
[a-z] หมายความว่า ข้อความที่นำามาเปรียบเทียบมีตัว
อักษรตัวใดตัวหนึ่งอยู่ระหว่าง a-z ก็จะให้ผลลัพธ์เป็น
จริง
[A-Z0-9] หมายความว่าข้อความที่นำามาเปรียบเทียบมี
ตัวอักษรตัวใดตัวหนึ่งเป็น ตัวอักษร A-Z หรือ ตัวเลข
0-9 ก็จะให้ผลลัพธ์เป็นจริง
ตัวอย่าง เช่น
“[s-z]ot“
57. 57
Using Regular Expression
สัญลักษณ์ที่ใช้ในการสร้างรูปแบบ
สัญลักษณ์ [ ]
หากมีสัญลักษณ์ ^ อยู่ในเครื่องหมาย [ ] นี้ จะให้
ความหมายตรงกันข้าม เช่น
“[^s-z]ot“
“http://www.tot.co.th” จะให้ผลลัพธ์เป็นเท็จ
“http://www.aot.co.th” จะให้ผลลัพธ์เป็นจริง
58. 58
Using Regular Expression
สัญลักษณ์ที่ใช้ในการสร้างรูปแบบ
สัญลักษณ์ ( )
ใช้ในการจัดกลุ่มตัวอักษรให้เป็นกลุ่มเดียวกันซึ่งหากมี
สัญลักษณ์ ^ นำาหน้าหรือมีสัญลักษณ์ $ | ? * + ตาม
หลัก) ก็จะประมวลผลเสมือนว่ากลุ่มตัวอักษรในวงเล็บนี้
เป็นตัวอักษรเดียวกัน เช่น
“(tot.)”
“http://www.tot.co.th” จะให้ผลลัพธ์เป็นจริง
“http://www.cat.co.th” จะให้ผลลัพธ์เป็นเท็จ
“http://www.tatoothai.com” จะให้ผลลัพธ์เป็นเท็จ
60. 60
Using Regular Expression
ฟังก์ชั่น ereg()
ใช้เปรียบเทียบข้อความรูปแบบ regular expression ที่
กำาหนดไว้ว่าสอดคล้องกันหรือไม่ ถ้าสอดคล้องกัน จะ
ให้ค่าเป็นจริง
รูปแบบ
Int ereg(string pattern, string string[, array regs])
pattern รูปแบบของ regular expression
string ข้อความที่ต้องการนำามาตรวจสอบ
regs ตัวแปร array ที่เก็บส่วนประกอบของข้อความ
ที่นำามาตรวจสอบ เฉพาะในส่วนที่สอดคล้องกับรูปแบบ
regular expression ที่อยู่ในเครื่องหมายวงเล็บ
61. 61
Using Regular Expression
Demo
<?#ereg01.php
$text = "http://www.tot.co.th";
$pattern = "^(http)://w{3}.[a-z][a-z0-9_-]*.(co.th|com|net)$";
if(ereg($pattern, $text, $regs)){
print("$text is valid.<br>");
foreach($regs as $ar_reg){
print("$ar_reg<br>");
}
}else{
print("Sorry! $text is not valid.<br>");
}
?>
62. 62
Using Regular Expression
ฟังก์ชั่น eregi()
ใช้เปรียบเทียบข้อความรูปแบบ regular expression ที่
กำาหนดไว้ว่าสอดคล้องกันหรือไม่ ถ้าสอดคล้องกัน จะ
ให้ค่าเป็นจริง เหมือน ereg() แต่จะถือว่าตัวอักษรพิมพ์
เล็กพิมพ์ใหญ่เหมือน (case insensitive)
รูปแบบ
Int eregi(string pattern, string string[, array regs])
63. 63
Using Regular Expression
Demo
<?#ereg02.php
$text = "http://www.TOT.co.th";
$pattern = "^(http)://w{3}.[a-z][a-z0-9_-]*.(co.th|com|net)$";
if(eregi($pattern, $text, $regs)){
print("$text is valid.<br>");
foreach($regs as $ar_reg){
print("$ar_reg<br>");
}
}else{
print("Sorry! $text is not valid.<br>");
}
?>
64. 64
Using Regular Expression
ฟังก์ชั่น ereg_replace()
ใช้แทนที่ข้อความย่อยที่สอดคล้องกับ regular expression ด้วย
ข้อความที่ระบุให้ case sensitive
รูปแบบ
string ereg_replace(string pattern, string replacement,
string string)
pattern รูปแบบของ regular expression
string ข้อความที่ต้องการนำามาตรวจสอบ
replacement ข้อความใหม่ที่ต้องการนำามาแทนที่
66. 66
Using Regular Expression
ฟังก์ชั่น eregi_replace()
ใช้แทนที่ข้อความย่อยที่สอดคล้องกับ regular expression ด้วย
ข้อความที่ระบุให้
โดย case insensitive
รูปแบบ
string eregi_replace(string pattern, string replacement,
string string)
pattern รูปแบบของ regular expression
string ข้อความที่ต้องการนำามาตรวจสอบ
replacement ข้อความใหม่ที่ต้องการนำามาแทนที่
68. 68
Using Regular Expression
ฟังก์ชั่น split()
ใช้แบ่งแยกข้อความที่ถูกคั่นด้วยสัญลักษณ์ หรือ
ข้อความย่อยที่สอดคล้องกับ regular expression และ
คืนค่าเป็น array
รูปแบบ
array split(string pattern, string string, [,int limit])
pattern รูปแบบของ regular expression
string ข้อความที่ต้องการนำามาแบ่ง
limit กำาหนดจำานวน element ของ array