Php: a simple method for obtaining images and DIV content on a webpage _ php instance

Source: Internet
Author: User
This article mainly introduces php's simple method of getting images and DIV content from webpages, which is implemented through regular expressions and powerful regular expressions, for more information, see 1. obtain all the images on the webpage:

The code is as follows:


<? Php
// Obtain the content of the specified address and store it to $ text
$ Text = file_get_contents ('http: // www.jb51.net /');

// Obtain all the img tags and store them in a two-dimensional array $ match
Preg_match_all ('/] *>/I', $ text, $ match );

// Print the match
Print_r ($ match );
?>

2. obtain the first image on the webpage:

The code is as follows:


<? Php
// Obtain the content of the specified address and store it to $ text
$ Text = file_get_contents ('http: // www.jb51.net /');

// Obtain the first img tag and store it in a two-dimensional array $ match
Preg_match ('/] *>/Ui', $ text, $ match );

// Print the match
Print_r ($ match );
?>

3. obtain the specific p block data on the specified webpage:

The code is as follows:


<? Php
// Obtain the content of the specified address and store it to $ text
$ Text = file_get_contents ('http: // www.jb51.net /');

// Remove line breaks and white spaces (serialization content is required)
// $ Text = str_replace (array ("/r", "/n", "/t", "/s"), '', $ text );

// Retrieve the content of the p tag with the id PostContent and store it in a two-dimensional array $ match
Preg_match ('/ ] * Id = "PostContent" [^>] *> (.*?) /Si', $ text, $ match );

// Print the match [0]
Print ($ match [0]);
?>

4. The combination of the above 2 and 3:

The code is as follows:


<? Php
// Obtain the content of the specified address and store it to $ text
$ Text = file_get_contents ('http: // www.jb51.net /');

// Retrieve the content of the p tag with the id PostContent and store it in a two-dimensional array $ match
Preg_match ('/ ] * Id = "PostContent" [^>] *> (.*?) /Si', $ text, $ match );

// Obtain the first img tag and store it in the two-dimensional array $ match2
Preg_match ('/] *>/Ui', $ text, $ match2 );

// Print out match2 [0]
Print_r ($ match2 [0]);
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.