Use preg_match_all in php to match the picture in the article _ PHP Tutorial

Source: Internet
Author: User
Use preg_match_all in php to match the image in the article. Preg_match_all function: intpreg_match_all (stringpattern, stringsubject, arraymatches [, intflags]) execute a global regular expression match to search all functions in subject that match pa preg_match_all:

Int preg_match_all (string pattern, string subject, array matches [, int flags]) executes a global regular expression match.
Search all the content that matches the regular expression given by pattern in the subject and put the result in the matches in the order specified by flags.
After the first match is found, the next search starts from the end of the previous match.
Flags can be a combination of the following tags (note that it is meaningless to combine PREG_PATTERN_ORDER and PREG_SET_ORDER ):
PREG_PATTERN_ORDER sorts the results so that $ matches [0] is an array of all pattern matching, $ matches [1] is an array consisting of strings matching the child pattern in the first bracket, and so on!

Example:

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:

The code is as follows:


Array
(
[0] => Array
(
[0] =>
[1] =>
[2] =>
)
[1] => Array
(
Http://www.jb51.net/usr/themes/dddefault/images/logo.png
Http://www.jb51.net/usr/uploads/2012/09/531656480.jpg
Http://www.jb51.net/usr/uploads/2012/09/2647136297.jpg
)
)

Http://www.bkjia.com/PHPjc/326445.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326445.htmlTechArticlepreg_match_all function: int preg_match_all (string pattern, string subject, array matches [, int flags]) execute a global regular expression matching all match pa in subject...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.