|
 |
|
จะแยก tag ของเว็บยังไงครับ จะเอามาเช็คลงในฐานข้อมูล |
|
 |
|
|
 |
 |
|
Code (PHP)
$str = '<a href="index.php?do=vote" target="_blank" class="list_server">ลิ้งค์ใช้โหวต</a>';
if(preg_match('/<(.+)>(.+)<\/(.+)>/', $str, $match)) {
$tag['attribute'] = trim($match[1]);
$tag['title'] = $match[2];
$tag['name'] = $match[3];
}
$arr = explode(' ', $tag['attribute']);
unset($arr[0]);
foreach($arr as $index => $val) {
$arr[$index] = trim($val);
}
$output = implode('&', $arr);
parse_str($output, $attribute);
foreach($attribute as $key => $val) {
$attribute[$key] = substr(stripslashes($val), 1, -1);
}
$tags[$tag['name']] = $attribute;
บรรทัดที่ 2: ตรวจสอบรูปแบบว่าถูกต้องรึไม่
บรรทัดที่ 3: ดึง a href="index.php?do=vote" target="_blank" class="list_server"
บรรทัดที่ 4: ดึง ลิ้งค์ใช้โหวต
บรรทัดที่ 5: ดึง a โดยในตัวแปรนี้จะเก็บเป็นชื่อของ tag ที่ทำการตรวจ
บรรทัดที่ 7: หั่นค่าที่เก็บไว้ในตัวแปรให้เป็น Array
บรรทัดที่ 8: ยกเลิก Array ตัวแรกที่หั่นมา โดยในที่นี้จะเป็น a
บรรทัดที่ 10: ตัดช่องว่าหน้าหลังของตัวแปร และแปลงเข้ารหัสไม่ให้อ่าน html (จาก & ก็จะเป็น &)
บรรทัดที่ 12: เอา Array มาเชื่อกันโดยตัวเชื่อมกัน
บรรทัดที่ 13: แปลงตัวแปรด้านบนให้เป็นรูปแบบ Array อีกทีหนึ่ง และเก็บไว้ที่ตัวแปร $attribute
บรรทัดที่ 15: เอาเครื่องหมาย \ ออก และเอา Double Quote (") ออก
บรรทัดที่ 17: กำหนดตัวแปล Array ให้จัดเก็บตามที่ต้องการ
ผลลัพธ์ที่ได้ออกมาจะออกมาแบบด้านล่าง
Code (PHP)
Array
(
[a] => Array
(
[href] => index.php?do=vote
[target] => _blank
[class] => list_server
)
)
ในตัวโปรแกรมข้างบนผมลองทำให้ออกมาตามแบบที่คุณ Widying ต้องการ แต่ยังมีส่วนที่ทำไม่ได้อยู่หลายจุดนะครับ
- หากใน Attribute มีช่องว่าง มันก็จะตัดเป็นตัวใหม่
- รับ Tag ได้ Tag เดียวในการรัน
ปล. ขออภัยหากมีข้อผิดพลาดด้วยนะครับ
|
ประวัติการแก้ไข 2011-05-24 14:28:01
 |
 |
 |
 |
Date :
2011-05-24 14:17:08 |
By :
TheBusTeD |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|