Spend 5 minutes to make a picture collector using php

WBOY
Release: 2016-08-08 09:30:29
Original
1216 people have browsed it

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 = &#39;&#39;;

    public function __construct($url){
       if( false === self::$web_content = file_get_contents($url)){
            self::$web_content = &#39;&#39;;
        }
    }
    static public function getImage(){
       if( &#39;&#39; != self::$web_content ){
            preg_match_all(&#39;/<img(.*?)src=\"([^\"]*)\"/i&#39;,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();
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template