กรณีศึกษา Web scraping กับเว็บพันทิพย์นี้เป็นตัวอย่างโค้ดง่ายๆ นะครับ ที่ใช้การเขียน regex (Regular expression)
มาให้แบ่งส่วนข้อความ
ผมคงไม่ร่ายอะไรมาก เอาโค้ดไปลองรันดู ตรงไหนไม่เข้าใจ
ให้เอาชื่อฟังค์ชั่น ไปต่อท้าย php.net/ เช่น php.net/file_get_contents
ไปเปิดใน browser ก็จะขึ้นข้อมูลของฟังค์ชั่นนั้นๆ มาให้ดูครับ
<?php
//Example code by EThaiZone
$page = file_get_contents("http://www.pantip.com/tech/market/price/index.php?NBK=on");
preg_match_all("#<tr[^>]+><td>(.*)</td><td>(.*)</td><td>(.*)</td></tr>#", $page, $matches);
echo "<h2>Notebook Price @ Pantip</h2><table><tr style='font-weight: bold; color: #000066;'><td>Brand</td><td>Detail</td><td>Price</td></tr>";
foreach($matches[0] as $key => $value) {
echo "<tr ".($key%2 ? "style='background-color: #eee;'" : "style='background-color: #bbb;'").">";
echo "<td>".$matches[1][$key]."</td>";
echo "<td>".$matches[2][$key]."</td>";
echo "<td>".$matches[3][$key]."</td>";
echo "</tr>";
}
echo "</table>";
?>
ข้อมูลของ Regex เบื้องต้น
Regular expressions
Some hints about the syntax of regular expressions:
Text:
. Any single character
[chars] One of chars
[^chars] None of chars
text1|text2 text1 or text2
Quantifiers:
? 0 or 1 of the preceding text
* 0 or N of the preceding text (N > 0)
+ 1 or N of the preceding text (N > 1)
Grouping:
(text) Grouping of text
Anchors:
^ Start of line anchor
$ End of line anchor
Escaping:
\ char escape that particular char
Pattern Matching metacharacter Definitions
\ Use before any of the following characters to escape or null the meaning or it. \* \. \$ \+ \[ \]
^ Start matching at this point
$ End point of the match
. Any character
[] Starts a range
| Starts alternative match this|that would mean match this or that
() starts a back reference point
? match 0 or 1 time Quantifier
+ match atleast 1 or more times Quantifier
* match 0 to infinite times Quantifier
{} match minimum to maximum Quantifier {0,3} match up to 3 times
Range Definitions []
^ Negates the class. [^A-Z]+ means don't match any uppercases
\ Use before any of the following characters to escape or null the meaning or it. [\+]+
- Range for matching [0-9]+ [a-zA-Z]+
อ้างอิงจาก
http://www.thaiseoboard.com/index.php/topic,3675.0.html 
อ่านเพิ่ม
http://th2.php.net/manual/en/regexp.reference.php
ผมเขียน regex จนโปรไปแล้ว

โค้ดของคุณคนจน ยังเอามาไม่หมดครับ
ยังไม่รู้ที่มาของ
$MPic_size $MPic_type เลย
ขอถามครับ พอจะมีความรู้ด้าน mySQL บ้างไหมครับ เช่น
1. การ optimize ให้ทำงานได้รวดเร็ว เช่น index
2. การ optimize ในส่วนของ Query หรืออื่นๆ
3. การใช้งานอื่นๆ ที่ควรทำหรือไม่ควรทำใน mysql
ถามมาากไปหน่อย แต่หลาย ๆคนคงอยากรู้ครับ จะได้นำไปพัฒนาเว็บ
เป็นคำถามที่ผมคิดว่าคงตอบไม่หมดครับ มันเยอะมากๆ
ผมคงตอบได้แค่ว่า (ตอบรวบๆ นะครับ)
1. อย่าลืมสิ่งที่ควรทำ พวกตั้ง primary key หรือการทำดัชนี (ก็ index นั้นแหละ)
หรือแม้แต่การเลือกชนิดของฟิลด์ให้เหมาะสมกับข้อมูล พวกนี้ก็สำคัญมาก
(อยากให้คุณ icez มาเขียนเรื่องนี้จริงๆ เขาเทพเรื่อง Mysql มากๆ)
2. ใช้ DB ให้เหมาะกับงาน เช่นเว็บฝากรูปที่มีการใช้ Mysql
ถ้าเราแค่ใช้ทำปกติ เช่นเก็บข้อมูลรูป อีเมล์ รหัสลบรูป อะไรพวกนี้
ก็สามารถซอย DB ให้เล็กลงเป็นหลายๆ table ได้
ข้อนี้อาจฟังดูยากๆ แต่พวกนี้ถ้าดูระบบออก สักวันเราจะสร้างระบบเป็น
(อย่ามองระบบเป็นโปรแกรมอย่างเดียว แต่ระบบคือกลุ่มการทำงานที่ทำไปพร้อมๆ กัน)
3. อะไรที่ใช้บ่อย ก็แคชไปเถอะ เช่นระบบ Tags ในบางเว็บ
เราอาจตั้งเวลาแคชนานๆ แม้ต้องแลกกับความสดของข้อมูลหน่อย
แต่สมมุติว่ามี PV ในหน้านั้นๆ สักหลักพัน ถ้าทำแคชไว้สัก 1 วัน
ก็ลดเวลาการ process โดยรวมได้เยอะแล้ว
มีหลายเว็บดังๆ ก็ใช้เทคนิคอื่นเข้าช่วย เช่นจากปากคำพี่ตี๋
เว็บส. ก็ใช้ memcache มาเก็บข้อมูลที่ใช้บ่อยๆ
แถมยังมีการสร้างระบบอื่นๆ เพิ่มมาใช้ เพื่อลดขนาดข้อมูลที่ต้ิองเก็บ
ซึ่งเป็นอัลกอริทึ่มเขียนเอง จากที่เคยคุยกันวันนั้น ถึงวันนี้ผมก็ยังไม่เก็ทสักที
เรื่องพวกนี้ผมและท่านๆ ก็จะไม่เก็ท จนกว่าจะได้ลองใช้จริงๆ

ปล. ผมจะไม่ตอบกระทู้นี้หลายวันหน่อยนะ เพราะงานค้างจัด รายงานด้วยอีก (ย้ำอีกที ผมยังเรียนป.ตรี อยู่ ^^")