Home >Backend Development >PHP Tutorial >A simple way to get pictures and DIV content in web pages with php_PHP Tutorial

A simple way to get pictures and DIV content in web pages with php_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:24:41910browse

1. Get all the pictures in the webpage:

Copy code The code is as follows:

//Get the content of the specified address and save it to $text
$text=file_get_contents('http://www.jb51.net/');

//Get all img tags and store them in the two-dimensional array $match
preg_match_all('/]*>/i', $text, $match);

//Print out match
print_r($match);
?>

2. Get the first picture in the webpage:

Copy code The code is as follows:

//Get the content of the specified address and save it to $text
$text=file_get_contents('http://www.jb51.net/');

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

//Print out match
print_r($match);
?>

3. Get specific div block data in the specified web page:

Copy code The code is as follows:

//Get the content of the specified address and save it to $text
$text=file_get_contents('http://www.jb51.net/');

//Remove newlines and whitespace characters (required for serialized content)
//$text=str_replace(array("/r","/n","/t","/s"), '', $text);

//Get the content of the div tag with the id PostContent and store it in the two-dimensional array $match
preg_match('/]*id="PostContent"[^>]*>(.*?) /si',$text,$match);

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

4. Combination of 2 and 3 above:

Copy code The code is as follows:

//Get the content of the specified address and save it to $text
$text=file_get_contents('http://www.jb51.net/'); 

//Take out the content of the div tag with the id PostContent and store it in the two-dimensional array $match
preg_match('/]*id="PostContent"[^>]*>(.*?) /si',$text,$match);

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825379.htmlTechArticle1. Get all the pictures in the webpage: Copy the code as follows: php //Get the content of the specified address, And save to $text $text=file_get_contents('http://www.jb51.net/'); //Get all...
Statement:
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