Share the simple method of obtaining images and DIV content in web pages using php, which is all implemented through regular expressions.
1. Get all the pictures in the webpage:
<?<span>php </span><span>//</span><span>取得指定位址的內容,并储存至 $text </span> <span>$text</span>=<span>file_get_contents</span>('http://www.jbxue.com/'<span>); </span><span>//</span><span>取得所有img标签,并储存至二维数组 $match 中 </span> <span>preg_match_all</span>('/<img[^>]*>/i', <span>$text</span>, <span>$match</span><span>); </span><span>//</span><span>打印出match </span> <span>print_r</span>(<span>$match</span><span>); </span>?>
2. Get the first picture on the webpage:
<?<span>php </span><span>//</span><span>取得指定位址的內容,并储存至 $text </span> <span>$text</span>=<span>file_get_contents</span>('http://www.jbxue.com/'<span>); </span><span>//</span><span>取得第一个 img 标签,并储存至二维数组 $match 中 </span> <span>preg_match</span>('/<img[^>]*>/Ui', <span>$text</span>, <span>$match</span><span>); </span><span>//</span><span>打印出match</span> <span>print_r</span>(<span>$match</span><span>); </span>?>
3. Get specific div block data in the specified web page:
<?<span>php </span><span>//</span><span>取得指定位址的內容,并储存至 $text </span> <span>$text</span>=<span>file_get_contents</span>('http://www.jbxue.com/'<span>); </span><span>//</span><span>去除换行及空白字符(序列化內容才需使用) //$text=str_replace(array("/r","/n","/t","/s"), '', $text); //取出 div 标签且 id 为 PostContent 的內容,并储存至二维数组 $match 中 </span> <span>preg_match</span>('/<div[^>]*id="PostContent"[^>]*>(.*?) <//div>/si',<span>$text</span>,<span>$match</span><span>); </span><span>//</span><span>打印出match[0]</span> <span>print</span>(<span>$match</span>[0<span>]); </span>?>
4. Combination of 2 and 3 above:
<?<span>php </span><span>//</span><span>取得指定位址的內容,并储存至 $text </span> <span>$text</span>=<span>file_get_contents</span>('http://www.jbxue.com/'<span>); </span><span>//</span><span>取出 div 标签且 id 为 PostContent 的內容,并储存至二维数组 $match 中 </span> <span>preg_match</span>('/<div[^>]*id="PostContent"[^>]*>(.*?) <//div>/si',<span>$text</span>,<span>$match</span><span>); </span><span>//</span><span>取得第一个 img 标签,并储存至二维数组 $match2 中 </span> <span>preg_match</span>('/<img[^>]*>/Ui', <span>$text</span>, <span>$match2</span><span>); </span><span>//</span><span>打印出match2[0] </span> <span>print_r</span>(<span>$match2</span>[0<span>]); </span>?>