Jump to content

Grab Data


3s2ng

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.
Link to comment
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]
Link to comment
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.


Link to comment
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
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.