1. Retrieve all images on the webpage:
Copy codeThe 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:
Copy codeThe 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 div block data on the specified webpage:
Copy codeThe 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 div tag whose id is PostContent and store it in a two-dimensional array $ match
Preg_match ('/<div [^>] * id = "PostContent" [^>] *> (.*?) <// Div>/si', $ text, $ match );
// Print the match [0]
Print ($ match [0]);
?>
4. The combination of the above 2 and 3:
Copy codeThe 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 div tag whose id is PostContent and store it in a two-dimensional array $ match
Preg_match ('/<div [^>] * id = "PostContent" [^>] *> (.*?) <// Div>/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]);
?>