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

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.