Php regular expression matching image path view preg_match_all () function usage

Source: Internet
Author: User
Php regular expression matching image path view preg_match_all () function usage. first look at the php code: $ ext = 'gif | jpg | jpeg | bmp | png '; // list the image suffixes to achieve multi-extension php regular matching image path to see the preg_match_all () function usage
Use the preg_match_all () function to view the php regular expression matching image path.

First look at the php code:

$ Ext = 'gif | jpg | jpeg | bmp | png '; // list Image suffixes to achieve multi-extension matching by http://www.k686.com green Software $ str ='

'; Preg_match_all ("/(href | src) = ([\" |']?) ([^ \ "'>] + \. ($ Ext) \ 2/I", $ str, $ matches); var_dump ($ matches );

?

The result of the above code execution is:

array(5) {  [0]=>  array(1) {    [0]=>    string(57) "src="http://www.k686.com/data/soft_img/2010091101619.jpg""  }  [1]=>  array(1) {    [0]=>    string(3) "src"  }  [2]=>  array(1) {    [0]=>    string(1) """  }  [3]=>  array(1) {    [0]=>    string(51) "http://www.k686.com/data/soft_img/2010091101619.jpg"  }  [4]=>  array(1) {    [0]=>    string(3) "jpg"  }}

?

?

Here we will review the function descriptions in the php manual:

Preg_match_all -- global regular expression matching description int preg_match_all (string pattern, string subject, array matches [, int flags]) 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 makes no sense to combine PREG_PATTERN_ORDER and PREG_SET_ORDER): PREG_PATTERN_ORDER sorts the results to make $ matches [0] an array that matches all modes, $ matches [1] is an array consisting of strings matching the child pattern in the first parentheses, and so on.
 ] +> (.*)
 ] +> | U ","Example:This is a test ", $ out, PREG_PATTERN_ORDER); print $ out [0] [0]. ",". $ out [0] [1]. "\ n"; print $ out [1] [0]. ",". $ out [1] [1]. "\ n";?> This example will output:Example:, This is a testexample:, this is a test. Therefore, $ out [0] contains a string that matches the entire pattern, and $ out [1] contains a string between a pair of HTML tags.

?

It may be a bit confused. continue the test. this time I used multiple image addresses, I can see it clearly.

 

'; Preg_match_all ("/(href | src) = ([\" |']?) ([^ \ "'>] + \. ($ Ext) \ 2/I", $ str, $ matches); var_dump ($ matches);?>

?

?

Result:

array(5) {  [0]=>  array(3) {    [0]=>    string(57) "src="http://www.k686.com/data/soft_img/2010091101619.jpg""    [1]=>    string(57) "src="http://www.k686.com/data/soft_img/2010091029938.jpg""    [2]=>    string(57) "src="http://www.k686.com/data/soft_img/2010092839019.jpg""  }  [1]=>  array(3) {    [0]=>    string(3) "src"    [1]=>    string(3) "src"    [2]=>    string(3) "src"  }  [2]=>  array(3) {    [0]=>    string(1) """    [1]=>    string(1) """    [2]=>    string(1) """  }  [3]=>  array(3) {    [0]=>    string(51) "http://www.k686.com/data/soft_img/2010091101619.jpg"    [1]=>    string(51) "http://www.k686.com/data/soft_img/2010091029938.jpg"    [2]=>    string(51) "http://www.k686.com/data/soft_img/2010092839019.jpg"  }  [4]=>  array(3) {    [0]=>    string(3) "jpg"    [1]=>    string(3) "jpg"    [2]=>    string(3) "jpg"  }}

?

?

Compared with the first simple single Image example, we know which element is needed. in general, we want to get the real and direct path of the image, that is, $ matches [3]. after obtaining the foreach, we can continue the following processing.

?

In fact, this code is relatively rigid and the regular expression can be changed:

Preg_match_all ('/

?

Others remain unchanged. because some abnormal website images do not have an extension, there is no such thing. Therefore, if you change the regular expression above, whatever the extension is, as long as it starts with img, it will not be a picture.

function getname($fileext){if( !in_array($fileext,array('gif','jpg','jpeg','bmp','png')) ) $fileext = 'png';return date('Ymdhis').rand(100, 999).'.'.$fileext;}

?

This is the suffix of the returned Image. if not, the default value is png.

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.