Sometimes we need to use php to get images from web pages and save them. Here we can use preg_match_all to match the image address. Coders in need can refer to
preg_match_all function:
int preg_match_all ( string pattern, string subject, array matches [, int flags] ) performs a global regular expression match, searches the subject for all content that matches the regular expression given by pattern and puts the results into matches in the order specified by flags.
After searching for the first match, subsequent searches start from the end of the previous match.
flags can be a combination of the following flags (note that it does not make sense to use PREG_PATTERN_ORDER and PREG_SET_ORDER together):
PREG_PATTERN_ORDER Sort the results so that $matches[0] is the array matching all patterns, and $matches[1] is the first bracket. An array of strings matched by the subpattern, and so on!
<?<span>php </span><span>$con</span> = <span>file_get_contents</span>("http://www.xxx.com/news/jb-1.html"<span>); </span><span>$pattern</span>="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/"<span>; </span><span>preg_match_all</span>(<span>$pattern</span>,<span>$con</span>,<span>$match</span><span>); </span><span>print_r</span>(<span>$match</span><span>); </span>?>
<span>Array</span><span>( [</span>0] => <span>Array</span><span> ( [</span>0] => <img src="http://www.xxx.com/usr/themes/dddefault/images/logo.png" alt="php preg match,php preg match all,filesmatch php,php preg match函数,php中preg match,match,puma match,match交友网站,itunes match订购,itunes match,服饰间的搭配match,mix match,match是什么意思,match函数,sid not matc" /><span> [</span>1] => <img title="码农教程上线了" src="http://www.xxx.com/usr/uploads/2012/09/531656480.jpg" alt="php preg match,php preg match all,filesmatch php,php preg match函数,php中preg match,match,puma match,match交友网站,itunes match订购,itunes match,服饰间的搭配match,mix match,match是什么意思,match函数,sid not matc" /><span> [</span>2] => <img src="http://www.xxx.com/usr/uploads/2012/09/2647136297.jpg" alt="php preg match,php preg match all,filesmatch php,php preg match函数,php中preg match,match,puma match,match交友网站,itunes match订购,itunes match,服饰间的搭配match,mix match,match是什么意思,match函数,sid not matc" /><span> ) [</span>1] => <span>Array</span><span> ( [</span>0] => http:<span>//</span><span>www.xxx.com/usr/themes/dddefault/images/logo.png</span> [1] => http:<span>//</span><span>www.xxx.com/usr/uploads/2012/09/531656480.jpg</span> [2] => http:<span>//</span><span>www.xxx.com/usr/uploads/2012/09/2647136297.jpg</span><span> ) )</span>
The above introduces how PHP uses preg_match_all to match the pictures in the article, including the content of match and PHP. I hope it will be helpful to friends who are interested in PHP tutorials.