Home > Backend Development > PHP Tutorial > PHP uses preg_match_all to match pictures in articles, _PHP tutorial

PHP uses preg_match_all to match pictures in articles, _PHP tutorial

WBOY
Release: 2016-07-12 08:54:07
Original
1276 people have browsed it

php uses preg_match_all to match the pictures in the article.

Sometimes we need to use php to get the pictures in the web page and save them. Here we can use preg_match_all to match the picture address. Coders who need it can refer to it

preg_match_all function:

int preg_match_all (string pattern, string subject, array matches [, int flags]) performs a global regular expression match
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:

<?<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>?> 
Copy after login

Result:

<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="码农教程" /><span>
            [</span>1] => <img>
            [</span>2] => <img>
        )
    [</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>
Copy after login

Original address: http://www.manongjc.com/article/708.html

Related reading:

php preg_match_all combined with str_replace to replace all img in the content

Analysis of the difference between php preg_match and preg_match_all functions

php preg_match matching examples of languages ​​in different countries

Solution to the problem of apache crash caused by PHP using preg_match_all

The meaning of /u, /i, /s in php preg_match regular matching

Problem with string length matched by php preg_match function

What does preg_match isU mean in php

Explanation on the use of php preg_match_all regular expression function

Explanation of php preg_match regular expression function example

javascript regular expression test() and exec() usage

Non-greedy pattern matching in php regular expressions

php regular expression efficiency greedy, non-greedy and backtracking analysis

Javascript regular expression greedy mode and non-greedy mode

Global matching pattern analysis of regular expressions in Javascript

javascript regular expression matching string string

js verifies (judges) whether it is a URL

PHP regular expression function usage examples

php preg_match function and php preg_match_al function l instances, methods, examples

php preg_match_all() function usage example

php preg_match regular expression function example

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1121388.htmlTechArticlephp uses preg_match_all to match the pictures in the article. Sometimes we need to use php to get the pictures in the web page and save them. Here we can use preg_match_all to match the image...
Related labels:
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