Home > Article > Backend Development > Spend 5 minutes to make a picture collector using php
The blogger is keen on various Internet technologies. He is often wordy and often accompanied by obsessive-compulsive disorder. He updates frequently. If you think the article is helpful to you, you can follow me. Please indicate "Dark Blue Sickle" when reprinting
The core of collection is regular matching. I am not particularly skilled in regular matching. There are many ways to capture img tags on the Internet, but my purpose is to capture the value of the src attribute in img. , and must satisfy greedy matching, otherwise regular matching will match as long a string as possible. All in all, it took me more than 5 minutes. . . However, I believe that students who are skilled in regular expressions can complete this collection in only 5 minutes.
<?php class Crawler{ static private $output = array(); static private $web_content = ''; public function __construct($url){ if( false === self::$web_content = file_get_contents($url)){ self::$web_content = ''; } } static public function getImage(){ if( '' != self::$web_content ){ preg_match_all('/<img(.*?)src=\"([^\"]*)\"/i',self::$web_content,self::$output); } } static public function output(){ var_dump(self::$output); } static public function render(){ foreach(self::$output[2] as $o){ echo "<img src=\"$o\">"; } } } $crawler = new Crawler('http://blog.csdn.net/hornedreaper1988'); $crawler::getImage(); //$crawler::output(); $crawler::render();
The above introduces how to use PHP to make a picture collector in 5 minutes, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.