Thursday 27 September 2018

php - Get contents of a div from a URL











I need to crawl through a page and get the contents of a particular div. I have php and javascript as my two main options. How can it be done?


Answer



This is quite a basic method to do it PHP and it returns the content in plain text. However you might consider revising the regex for your particular need.



  $link = file_get_contents("http://www.domain.com");

$file = strip_tags($link, "
");
preg_match_all("/
(?:[^<]*)<\/div>/is", $file, $content);
print_r($content);
?>

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...