Im not sure what Im doing wrong here but basically this script scrapes pages. The array has 3 urls to scrape but it doesnt work.

Could someone please show me where Im going wrong.

<?php

$req_url = array('http://www.first.com', 'http://www.second.com', 'http://www.third.com');

function get_head($a,$b,$c){ // Gets a string between 2 strings
        $y = explode($b,$a);
        $x = explode($c,$y[1]);
        return $x[0];
    } 

function get_body($a,$b,$c){ // Gets a string between 2 strings
        $y = explode($b,$a);
        $x = explode($c,$y[1]);
        return $x[0];
    }

//Get head
echo get_head(file_get_contents('$req_url'), '<head>', '</head>');

//Gets body
echo get_body(file_get_contents('$req_url'), '<body>', '</body>');
 

?>

Thanks :)

Firstly, you have two functions that do exactly the same thing. Secondly, you're passing an array to a function file_get_contents that accepts a string.

<?php
$req_url = array('http://www.first.com', 'http://www.second.com', 'http://www.third.com');

function get_inner_string($a,$b,$c) // Gets a string between 2 strings
{
  $y = explode($b,$a);
  $x = explode($c,$y[1]);
  return $x[0];
} 

foreach($req_url as $url) {
  $page = file_get_contents($url);
  echo get_inner_string($page, '<head>','</head>');
  echo get_inner_string($page, '<body>','</body>');
}
?>

That works perfectly. Thank you.

However I changed $x = explode($c,$y[1]); to $x = explode($c,$y[0]); .

This article has been dead for over six months. Start a new discussion instead.