Jump to content


Photo

Grab Data


  • Please log in to reply
6 replies to this topic

#1 3s2ng

3s2ng
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 01 September 2006 - 05:14 AM

Hello Freaks,

I Want to grab the content of a certain site an I want to store the Name and the price of a certain product in an array but I dont get the expression to match a certain tags.

Here is an example tag from the site.

 
<tr>
       <td><h2><a href="viewitem.php?iid=586084">InnoDV-1000LE</a></h2></td>
        <td class="catprice"><h3>P15000.00</h3></td>
   </tr>

What I want is to save the "iid" value, name and the price.

Any help would be greatly appriciated.

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 01 September 2006 - 01:51 PM

<pre>
<?php
	$data = <<<DATA
	<tr>
       <td><h2><a href="viewitem.php?iid=586084">InnoDV-1000LE</a></h2></td>
        <td class="catprice"><h3>P15000.00</h3></td>
   </tr>
DATA;
	
	preg_match('%iid=(\d+)">([^<]+).+?<h3>([^<]+)%s', $data, $matches);
	array_shift($matches);
	print_r($matches);
?>
</pre>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 3s2ng

3s2ng
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 02 September 2006 - 02:27 AM

@effigy

Thanks for the reply. I works great.

But I have one more question. I want to grab a dynamic content. I created a code like this adapting your regex


  //$URL = "http://localhost/functions/tpc.php";
  $URL = "http://tipidpc.com/catalog.php?cat=14";
 
  $file = fopen("$URL", "r");
  while (!feof($file)) {
        $data = fgets($file, 4096);
	preg_match('%iid=(\d+)">([^<]+).+?<h3>([^<]+)%s', $data, $matches);
	array_shift($matches);
	print_r($matches);
	
	}

What i got is an array without values. How can I modify it in such a way that it will save all the contents using that pattern in an array and basically display them in a table.

Thanks in advance. I greatly appreciate all you help.




#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 03 September 2006 - 03:31 AM

You may want to try file_get_contents. Once the match succeeds, you can access them via the $matches array.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 3s2ng

3s2ng
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 03 September 2006 - 04:06 AM

Sorry for so many questions.

I'm just new here and only a month experience in PHP. That why every comments and help would be greatly appreciated...

I've read about file_get_contents fucntions and it will return strings.

$URL = "http://tipidpc.com/catalog.php?cat=14";
 
  $file = fopen("$URL", "r");
  while (!feof($file)) {
        $data = fgets($file, 4096);
	preg_match('%iid=(\d+)">([^<]+).+?<h3>([^<]+)%s', $data, $matches);
	array_shift($matches);
	print_r($matches);
	
	}

Can you show me the code base from my previous codes how to save the data into an array.

The result I want is something like

Array
(
    [0] => 587067
    [1] => PCMCIA desktop PCI slot card
    [2] => P1500.00
)
Array
(
    [0] => 1233
    [1] => Blahhh
    [2] => P1500.00
)
Array
(
    [0] => 587067
    [1] => PCvblah blah ab
    [2] => P1500.00
)
and so on.

Thanks


#6 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 03 September 2006 - 06:15 AM

If you want all the matches, you must use preg_match_all. The PREG_SET_ORDER flag keeps your data grouped by match (all the backreferences together), instead of individual matches (each backreference separate).

<pre>
<?php

	$URL = 'http://www.tipidpc.com/catalog.php?cat=14';
	$contents = file_get_contents($URL);
	preg_match_all('%iid=(\d+)">([^<]+).+?<h3>([^<]+)%s', $contents, $matches, PREG_SET_ORDER);
	foreach ($matches as &$match) {
		array_shift($match);
	}
	print_r($matches);
?>
</pre>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#7 3s2ng

3s2ng
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 03 September 2006 - 08:40 AM

Thanks effigy,

It works wonder.

Wow! I love to be freak!

Thanks Again!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users