jQueryTips by Tee++;

  • Home
  • Videos
  • jQueryTips's Fans

Coding like playing a piano.

Subscribe RSS

Search

Search

Sponsored

jQueryTips on Social

Followers

hide
Chainnnn Pang25441 Dexteri@n Ton KanexKane Supachai JustDoIt Rut petchy Tophit anndrew Webmaster

Categories

hide
  • Programing
  • Database
  • Framework
  • Mobile Development
    • Titanium Mobile
    • PhoneGap
  • Life

Tag Cloud

hide
PHP JavaScript MySQL XML Mobile jQuery CodeIgniter Hash php xml Debug Library Zend API Events Plugins Facebook Basic Registration Tool Twitter Search Swath Performance ffmpeg YouTube Web Service Yahoo! NoSQL MongoDB PhoneGap

Recent Posts

hide
  • ใครๆ ก็มี Utils เอางี้เราเพิ่ม Utils ให้กับ CodeIgniter กันบ้าง
  • แนะนำ JS.Class (A JavaScript class library)
  • PHP กับ OOP มาปูพื้นฐานกันเสียใหม่ก่อน
  • ประกาศข่าวสารจาก Admin
  • JSFIDDLE สุดยอด Tools สำหรับการ Debug การเขียนตัวอย่างโคด
  • Titanium Mobile + HTML + Native Scrolling
  • HTML to PDF ตัวไหนดี วันนี้ผมมีคำตอบ ...
  • ทำความรู้จักกับ JavaScript Pattern ในเชิงลึก
  • ผมบอกคุณแล้ว .... คนโง่ก็ Code ได้
  • วันนี้ มาออกแบบ Web Service ด้วย PHP กันเถอะ (CodeIgniter Version)

Recent Comments

hide
  • เยดดดด
  • มีตัวอย่าง CI แบบ เพียว ๆ ไหมครับ
  • ขอบคุณมากครับ เห็นแนวทางได้ชัดขึ้นจากตอนแรก ที่งงไปหมด ไม่รู้อะไรเป็นอะไร
  • ขอบคุณมากเลยครับท่าน....แหม่เพิ่มกำลังใจให้ผมได้เยอะเลย ตอนนี้กำลังฝึกงานอยู่ที่แห่งนึงในหาดใหญ่ พร้อมเด็กจากอีกสถาบันนึง 3 คน เราคิดว่าเรา…
  • Very good blog, I really like ~

Links

hide
  • OSCOOL
  • Architeture & Performance
  • Video and TV
  • Appcelerator
  • PhoneGap
  • Home
  •  » Blogs

Simple HTML DOM Parser มันคือ jQuery's selectors ดีๆ นี่เอง

Apr 21, 2009 11:27:45 PM | 0 Comments | in Programing | PHP
ไปเจอของดีมาครับ เป็น PHP Library ที่ทำงานในลักษณะ คล้ายๆกับ Selectors ของ jQuery แต่ว่าเป็น PHP ซึ่งเรียกให้ถูกมันก็คือ HTML DOM Parser น่ะแหละครับ ซึ่งการใช้งานค่อนข้างง่าย เล่นเอาลืม Regexp ไปเลย

เดี๋ยวนี้ เว็บไซด์ส่วนใหญ่นั้น จะเขียนกันในรูปแบบ XHTML ซึ่งมักจะมาการระบุ Class หรือ Id ให้กับ Elements เกือบทั้งหมดอยู่แล้ว ซึ่งบางที ถ้าเราจะทำการ Get Content ที่อยู่ในช่วง tag ของมัน เราก็มักจะใช้ความสามารถของ Regular Expression (Regexp) โดยส่วนใหญ่ แต่.....

ชีวิตคุณจะสบายขึ้น ถ้าไปรู้จักกับ Lib ตัวนี้ ที่จะไม่ต้องมานั่งปวดหัวกับการคิด regexp อีกต่อไป เพราะว่า มันเขียนมาให้เกือบหมดแล้ว

เท่าที่ไปลองไล่ lib มาก็เห็นว่าใช้ความสามารถของ regexp เหมือนกัน แต่ทว่า มันเขียบนได้ เทพ แท้ๆ เลยครับ

อย่่างสมมุติผมจะภาพทั้งหมดจาก Google ผมก็เขียนแค่

<?php
include(’simple_html_dom.php’);
$dom = file_get_html(‘http://www.google.com’);
foreach ($dom->find(‘img’) as $elm)
  echo $elm->src."
";
?>

นอกจจากนี้มันยังทำกาเปลี่ยนแก้ไขค่าได้อย่างสะดวก เช่น
<?php
// edit class
$dom->find(‘div[id=hello]‘, 0)->class = "newclassname";
 
// edit inner html
$dom->find(‘div’, 1)->innertext = "new content";
 
// PS. 0, 1 are index of elements
?>

ดึงแค่ text โดยตัด HTML Tags

<?php
echo file_get_html(‘http://www.google.com/’)->plaintext;
?>

นอกจากนี้ยังทำงานได้อีกเพียบเช่น หา Children, childNodes, parent, siblings เรียกว่า เมพ ขิงๆ ได้เลยทีเดียว

Recommended by: K.Auttasak
Download: http://simplehtmldom.sourceforge.net/
API Reference: http://simplehtmldom.sourceforge.net/manual_api.htm

twitter stumbleupon delicious digg facebook

0 comments

Add comment Load previous All comments

Leave a comment

Post Comment

Powered by OSCOOL. You may view this on RSS or ATOM.

OSCOOL

  • Twitter
  • Facebook
  • Next