php USES preg_match_all to match the images in the article

  • 2020-05-30 19:42:01
  • OfStack

preg_match_all function:

int preg_match_all (string pattern, string subject, array matches [, int flags]) performs a global regular expression match
Search subject for all matches to the regular expressions given by pattern and place the results in matches in the order specified by flags.
After the first match is found, the next search begins at the end of the previous match.
flags can be a combination of the following tags (note that PREG_PATTERN_ORDER and PREG_SET_ORDER do not make sense together) :
PREG_PATTERN_ORDER sorts the results so that $matches[0] is an array of all pattern matches, $matches[1] is an array of strings matched by the first bracketed subpattern, and so on!

Example:


<?php
$con = file_get_contents("https://www.ofstack.com/news/jb-1.html");
$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
preg_match_all($pattern,$con,$match);
print_r($match);
?> 

Results:


Array
(
    [0] => Array
        (
            [0] => <img src="https://www.ofstack.com/usr/themes/dddefault/images/logo.png" alt=" Script school " />
            [1] => <img style="display: block; margin-left: auto; margin-right: auto;" title=" The script school is online " src="https://www.ofstack.com/usr/uploads/2012/09/531656480.jpg" alt=" The script school is online 2" />
            [2] => <img style="display: block; margin-left: auto; margin-right: auto;" src="https://www.ofstack.com/usr/uploads/2012/09/2647136297.jpg" alt="875EA1C00E50B4542797E24FA6E7E1F2.jpg" />
        )
    [1] => Array
        (
            [0] => https://www.ofstack.com/usr/themes/dddefault/images/logo.png
            [1] => https://www.ofstack.com/usr/uploads/2012/09/531656480.jpg
            [2] => https://www.ofstack.com/usr/uploads/2012/09/2647136297.jpg
        )
)


Related articles: