Jump to content

[SOLVED] Scraper and Preg Match


atticus

Recommended Posts

Hi all,

 

I have never used a scraper before, but I need to automate a process and this is where I am so far:

 

<?php
$data = file_get_contents('http://www.example.com/');
$regex = '/\/screenshots\/website-templates\/(.+?)-m/';
preg_match($regex,$data,$match);
var_dump($match);
echo $match[1];
?>

 

I am able to retrieve one piece of data with this script... I need it to grab all nine of the numbers and put it into an array...

 

Also, I am not sure how to pull data out of the array. Ideally, I want to run a while loop and insert each snippet of data into a database. Any ideas or sources I could work from...thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/171044-solved-scraper-and-preg-match/
Share on other sites

<?php
foreach ($match[1] as $digit) {
echo $digit;
}
?>

 

Since $match[1] is an array of the first parenthesized matches. And if you're grabbing digits only, you can specify that in your pattern:

 

'~/screenshots/website-templates/([0-9]+)-m~'

 

By using another pattern delimiter we don't need to escape the slashes.

Archived

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

×
×
  • 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.