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]);
?>