Current location: Hot Scripts Forums » Programming Languages » PHP » Help extracting data from html and writing it to mysql?


Help extracting data from html and writing it to mysql?

Reply
  #1 (permalink)  
Old 06-10-08, 11:58 PM
method method is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
Help extracting data from html and writing it to mysql?

Hi all. i wonder how i can extract the following data from an html file and set them to variables so i can write them to mysql database:

Data to extract :

1)http://www.someurl.com/
2)12345678
3)http://www.somesite.com/pic1.jpg
4)Tom

Note: the number of these set of data is not fixed!! Some times it reaches 900!!

So i be happy if some one show me a solution that works for large sets of data and allow me write them to mysql db.Thanks


Quote:
<tr>
<td><a href="http://www.someurl.com/">12345678</a></td>
<td><img src="http://www.somesite.com/pic1.jpg" alt="" /></td>
<td>Tom</td>
</tr>

HTML 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>
	<title>title here</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" type="text/css" href="http://www.locahost.com/res/style.css" />
</head>
<body id="export">
	<div id="header">
		<h1>List</h1>
	</div>

	<table id="friends" width="100%" border="1">
		<thead>
			<tr>
				<th>ID</th>
				<th width="90">Picture</th>
				<th>Name</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td><a href="http://www.someurl.com/">12345678</a></td>
				<td><img src="http://www.somesite.com/pic1.jpg" alt="" /></td>
				<td>Tom</td>
			</tr>

	                <tr>
				<td><a href="http://www.someurl2.com/">87654321</a></td>
				<td><img src="http://www.somesite2.com/pic2.jpg" alt="" /></td>
				<td>jerry</td>
			</tr>



		</tbody>
	</table>
	<br />
</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-12-08, 11:40 AM
nashruddin nashruddin is offline
Newbie Coder
 
Join Date: Jun 2008
Location: Indonesia
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
that's a very complicated task. but here's how to parse the links:

PHP Code:

$html file_get_contents('http://www.example.com');

$re="<a\s[^>]*href\s*=\s*(['\"]??)([^'\">]*?)\\1[^>]*>(.*)<\/a>";
preg_match_all("/$re/siU"$html$matches);
$links $matches[2];

print_r($links);
/*
will print:
Array
{
   [0] => http://www.someurl.com
   [1] => http://www.somesite.com/pic1.jpg
}
*/ 
---
Cool PHP programmer writing cool PHP scripts

Last edited by Nico; 06-14-08 at 03:42 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-12-08, 12:53 PM
method method is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for your reply. But is there any way to get the 2)12345678 and 3) tom ? I need those very badly :-(
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
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 11:25 AM
writing form data to a text file topside Script Requests 2 03-02-07 03:28 AM
Help with writing to a html file rameez PHP 5 07-21-06 01:53 AM
need help writing script to let user edit thier html or php files pinochio53 PHP 14 12-15-03 08:26 AM


All times are GMT -5. The time now is 12:42 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.