请看代码
$content = '今天天气不错,准备去郊游。<img class="t-image" src="http://www.g.com/1.jpg"/>今天还是不辞而别,没有什么也别的<img src="http://www.g.com/2a.jpg"/>花开多日无人看<img class="t-image" src="http://www.g.com/k.png"/>';
$reg_tag = '/<img.*?\"([^\"]*(jpg|bmp|jpeg|gif|png)).*?>/';
$ret = preg_match_all($reg_tag, $content, $match_result, PREG_PATTERN_ORDER);
var_dump($match_result);
flag选项值:
1、PREG_PATTERN_ORDER:结果排序为$matches[0]保存完整模式的所有匹配, $matches[1] 保存第一个子组的所有匹配,以此类推。
2、REG_SET_ORDER::结果排序为$matches[0]包含第一次匹配得到的所有匹配(包含子组), $matches[1]是包含第二次匹配到的所有匹配(包含子组)的数组,以此类推。
3、PREG_OFFSET_CAPTURE: 如果这个标记被传递,每个发现的匹配返回时会增加它相对目标字符串的偏移量。
注意:PREG_PATTERN_ORDER 和 PREG_SET_ORDER 不能同时使用,如果没有指定排序的标记,则默认为 PREG_PATTERN_ORDER
// PREG_PATTERN_ORDER (默认值)
array (size=3)
0 =>
array (size=3)
0 => string '<img class="t-image" src="http://www.g.com/1.jpg"/>' (length=51)
1 => string '<img src="http://www.g.com/2a.jpg"/>' (length=36)
2 => string '<img class="t-image" src="http://www.g.com/k.png"/>' (length=51)
1 =>
array (size=3)
0 => string 'http://www.g.com/1.jpg' (length=22)
1 => string 'http://www.g.com/2a.jpg' (length=23)
2 => string 'http://www.g.com/k.png' (length=22)
2 =>
array (size=3)
0 => string 'jpg' (length=3)
1 => string 'jpg' (length=3)
2 => string 'png' (length=3)
// REG_SET_ORDER
array (size=3)
0 =>
array (size=3)
0 => string '<img class="t-image" src="http://www.g.com/1.jpg"/>' (length=51)
1 => string 'http://www.g.com/1.jpg' (length=22)
2 => string 'jpg' (length=3)
1 =>
array (size=3)
0 => string '<img src="http://www.g.com/2a.jpg"/>' (length=36)
1 => string 'http://www.g.com/2a.jpg' (length=23)
2 => string 'jpg' (length=3)
2 =>
array (size=3)
0 => string '<img class="t-image" src="http://www.g.com/k.png"/>' (length=51)
1 => string 'http://www.g.com/k.png' (length=22)
2 => string 'png' (length=3)
// PREG_OFFSET_CAPTURE
array (size=3)
0 =>
array (size=3)
0 =>
array (size=2)
0 => string '<img class="t-image" src="http://www.g.com/1.jpg"/>' (length=51)
1 => int 39
1 =>
array (size=2)
0 => string '<img src="http://www.g.com/2a.jpg"/>' (length=36)
1 => int 138
2 =>
array (size=2)
0 => string '<img class="t-image" src="http://www.g.com/k.png"/>' (length=51)
1 => int 195
1 =>
array (size=3)
0 =>
array (size=2)
0 => string 'http://www.g.com/1.jpg' (length=22)
1 => int 65
1 =>
array (size=2)
0 => string 'http://www.g.com/2a.jpg' (length=23)
1 => int 148
2 =>
array (size=2)
0 => string 'http://www.g.com/k.png' (length=22)
1 => int 221
2 =>
array (size=3)
0 =>
array (size=2)
0 => string 'jpg' (length=3)
1 => int 84
1 =>
array (size=2)
0 => string 'jpg' (length=3)
1 => int 168
2 =>
array (size=2)
0 => string 'png' (length=3)
1 => int 240