Scrape HTML With PHP

PHP Scrape HTML

The example below shows how to scrape a website to get a list of all Javascript files for a specific web page.

$data = file_get_contents('http://example.com');

//$regex ='/script type="text\/javascript" src="\/media\/system\/js\/mootools.js">/'; // gets everything in script except beginning < and ending </script>
$regex = '/<script(.*)<\/script>/'; //finds all jscripts and displays everything buy beginning <script tags and ending </script> tag and WORKS

preg_match_all($regex,$data,$posts, PREG_SET_ORDER);
	
	print_r($posts);
	
	echo '<br/>';

	$cnt = count($posts);
	echo $cnt;
for($i=0; $i < $cnt; $i++){
foreach ($posts[$i] as $post) {
  	
if(strstr($post, 'text/javascript')){
echo "hi".$post."<br/>";
}else{
    // do something with data
	//echo "hoy".$post."<br/>";
	}
}
}