atticus Posted August 19, 2009 Share Posted August 19, 2009 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 Quote Link to comment Share on other sites More sharing options...
atticus Posted August 19, 2009 Author Share Posted August 19, 2009 changed preg_match to preg_match_all() which got all of the instances...still don't know how to use the data in the array as of yet. Quote Link to comment Share on other sites More sharing options...
atticus Posted August 19, 2009 Author Share Posted August 19, 2009 based upon php.net, I used this code: foreach ($match as $color) { echo "Do you like $color?\n"; } Unfortunately, the output is only: Do you like Array? Do you like Array? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 19, 2009 Share Posted August 19, 2009 <?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. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 19, 2009 Share Posted August 19, 2009 thebadbad beat me to it. Atticus, note that when using preg_match_all, this automatically creates a multi-dimensional array.. so simply scanning the base array won't suffice. Quote Link to comment Share on other sites More sharing options...
atticus Posted August 19, 2009 Author Share Posted August 19, 2009 Thanks guys...this really clears it up for me...I am able to now use my script! thanks. 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.