3s2ng Posted September 1, 2006 Share Posted September 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 1, 2006 Share Posted September 1, 2006 [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] Quote Link to comment Share on other sites More sharing options...
3s2ng Posted September 2, 2006 Author Share Posted September 2, 2006 @effigyThanks 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 3, 2006 Share Posted September 3, 2006 You may want to try[tt] file_get_contents[/tt]. Once the match succeeds, you can access them via the $matches array. Quote Link to comment Share on other sites More sharing options...
3s2ng Posted September 3, 2006 Author Share Posted September 3, 2006 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted September 3, 2006 Share Posted September 3, 2006 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] Quote Link to comment Share on other sites More sharing options...
3s2ng Posted September 3, 2006 Author Share Posted September 3, 2006 Thanks effigy,It works wonder.Wow! I love to be freak!Thanks Again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.