Jump to content

Archived

This topic is now archived and is closed to further replies.

3s2ng

Grab Data

Recommended Posts

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.

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

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

Any help would be greatly appriciated.

Share this post


Link to post
Share on other sites
[code]
<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>
[/code]

Share this post


Link to post
Share on other sites
@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

[code]

  //$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);

}[/code]

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.


Share this post


Link to post
Share on other sites
You may want to try[tt] file_get_contents[/tt]. Once the match succeeds, you can access them via the $matches array.

Share this post


Link to post
Share on other sites
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.

[code]$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);

}[/code]

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

Share this post


Link to post
Share on other sites
If you want all the matches, you must use[tt] preg_match[b]_all[/b][/tt]. The[tt] PREG_SET_ORDER [/tt] flag keeps your data grouped by match (all the backreferences together), instead of individual matches (each backreference separate).

[code]
<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>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.