Home > Backend Development > PHP Tutorial > Use preg_match_all in php to match pictures in articles_PHP tutorial

Use preg_match_all in php to match pictures in articles_PHP tutorial

WBOY
Release: 2016-07-21 15:13:51
Original
870 people have browsed it

preg_match_all function:

int preg_match_all (string pattern, string subject, array matches [, int flags]) performs a global regular expression matching
Search in subject for all regular expressions given by pattern What the expression matches and the results are placed in matches in the order specified by flags.
After the first match is found, 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 of all pattern matches, and $matches[1] is the array of all pattern matches. An array of strings matched by the subpattern in parentheses, and so on!

Example:

Copy code The code is as follows:

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

Result:

Copy code The code is as follows:

Array
(
[0] => ) > .jb51.net/usr/uploads/2012/09/531656480.jpg" alt="Scripting School is online 2" />
                                                                                                                              left: auto; margin-right: auto;" src="http://www.jb51.net/usr/uploads/2012/09/2647136297.jpg" alt="875EA1C00E50B4542797E24FA6E7E1F2.jpg" />
)
[1] => Array
(
[0] => http://www.jb51.net/usr/themes/dddefault/images/logo.png
[1] => http://www.jb51.net/usr/uploads/2012/09/531656480.jpg
      [2] => http://www.jb51.net/usr/uploads/2012/09 /2647136297.jpg
)
)



http://www.bkjia.com/PHPjc/326445.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326445.htmlTechArticlepreg_match_all function: int preg_match_all (string pattern, string subject, array matches [, int flags]) executes a global regular expression Expression matching searches all subjects with pa...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template