Current location: Hot Scripts Forums » Programming Languages » PHP » Eliminating HTML element


Eliminating HTML element

Reply
  #1 (permalink)  
Old 04-17-09, 02:19 PM
ben777 ben777 is offline
Newbie Coder
 
Join Date: Nov 2005
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Eliminating HTML element

Hello guys,

I'm developing a back-end system which need to take output (an html page with table) from another website using cURL function. The problem is, i don't need all HTML element from that page. I just want to take some. Here is example of the HTML page :

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et justo. In hac habitasse platea dictumst. Etiam fermentum, erat id.</p>

<br />

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th scope="col">Header 1 </th>
    <th scope="col">Header 2 </th>
    <th scope="col">Header 3 </th>
    <th scope="col">Header 4 </th>
  </tr>
  <tr>
    <td>Data 1.1 </td>
    <td>Data 2.1</td>
    <td>Data 3.1</td>
    <td>Data 4.1</td>
  </tr>
  <tr>
    <td>Data 1.2</td>
    <td>Data 2.2</td>
    <td>Data 3.2</td>
    <td>Data 4.2</td>
  </tr>
  <tr>
    <td>Data 1.3</td>
    <td>Data 2.3</td>
    <td>Data 3.3</td>
    <td>Data 4.3</td>
  </tr>
  <tr>
    <td>Data 1.4</td>
    <td>Data 2.4</td>
    <td>Data 3.4</td>
    <td>Data 4.4</td>
  </tr>
</table>

<br />

<br />

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th scope="col">Header 1 </th>
    <th scope="col">Header 2 </th>
    <th scope="col">Header 3 </th>
  </tr>
  <tr>
    <td>Data 1.1 </td>
    <td>Data 2.1</td>
    <td>Data 3.1</td>
    <td>Data 4.1</td>
  </tr>
  <tr>
    <td>Data 1.2</td>
    <td>Data 2.2</td>
    <td>Data 3.2</td>
    <td>Data 4.2</td>
  </tr>
  <tr>
    <td>Data 1.3</td>
    <td>Data 2.3</td>
    <td>Data 3.3</td>
    <td>Data 4.3</td>
  </tr>
</table>

</body>
</html>
Simple plain HTML... All i want to do is eliminating all except the second table. I tried using str_replace function but there are too many element to eliminate. Is there any suggestion for me ?? Thanks in advance...


ben
Reply With Quote
  #2 (permalink)  
Old 04-17-09, 02:47 PM
de.monkeyz's Avatar
de.monkeyz de.monkeyz is offline
Wannabe Coder
 
Join Date: Apr 2008
Location: Leeds, UK
Posts: 116
Thanks: 0
Thanked 0 Times in 0 Posts
This might work on the string.

PHP Code:

$matched = array();
preg_match_all('/<table.*?<\/table>/si'$html$matched);

print_r($matched); 
Check which key in the array is the table you want.
__________________
Wanna learn AJAX? Goto http://www.deathmonkeyz.com/tutorials for free video tutorials.

AJAX - Lesson 11 - AJAX Guestbook (23/8/08)
C++ - Lesson 10 - Classes (24/8/08)
JavaScript - Lesson 03 - The DOM (24/8/08)
Need an AJAX app? Look no further, I'm available for work
Reply With Quote
  #3 (permalink)  
Old 04-17-09, 08:02 PM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
The page you posted if valid XHTML so yo could use the DOM functions.
http://www.php.net/book.dom

If any page you want to parse is not valid xml, you'll have to revert to string manipulations.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
html tutoral thefrtman HTML/XHTML/XML 5 04-27-09 10:25 AM
[SOLVED] HTML element with DOM zoliky JavaScript 8 05-20-08 05:20 PM
[SOLVED] delay on html element properties? UnrealEd JavaScript 5 05-05-08 03:23 AM
un-hiding a html element... darkcarnival JavaScript 2 04-15-07 07:45 AM
I most definately suggest DevelopingCentral.com For Any Website Design/Development! Salty777 General Advertisements 2 10-01-04 04:27 AM


All times are GMT -5. The time now is 08:32 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.