Home > Backend Development > PHP Tutorial > PHP regular extracts the attribute values ​​​​of the img tag

PHP regular extracts the attribute values ​​​​of the img tag

巴扎黑
Release: 2016-11-24 13:49:54
Original
1626 people have browsed it

<?php
/*
create by tuzwu@qq.com for http://www.xiaojudeng.com
*/
$ext = &#39;gif|jpg|jpeg|bmp|png&#39;;//罗列图片后缀从而实现多扩展名匹配 by http://www.k686.com 绿色软件
$str = &#39;<p><img title="小桔灯分类信息网" alt="小桔灯分类信息网" onload="ResizeImage(this,860)" src="http://www.xiaojudeng.com/uploadfile/2011/0910/20110910102454887.jpg" /></p><p><img title="小桔灯分类信息网" alt="小桔灯分类信息网" onload="ResizeImage(this,860)" src="http://www.xiaojudeng.com/uploadfile/2011/0910/20110910102455105.jpg" /></p><p><img title="小桔灯分类信息网" alt="小桔灯分类信息网" onload="ResizeImage(this,860)" src="http://www.xiaojudeng.com/uploadfile/2011/0910/20110910102459367.jpg" /></p>&#39;;
$list = array();//这里存放结果map
$c1 = preg_match_all(&#39;/<img\s.*?>/&#39;, $str, $m1);//先取出所有img标签文本
for($i=0; $i<$c1; $i++) {//对所有的img标签进行取属性
$c2 = preg_match_all(&#39;/(\w+)\s*=\s*(?:(?:(["\&#39;])(.*?)(?=\2))|([^\/\s]*))/&#39;, $m1[0][$i], $m2);//匹配出所有的属性
for($j=0; $j<$c2; $j++) {//将匹配完的结果进行结构重组
$list[$i][$m2[1][$j]] = !empty($m2[4][$j]) ? $m2[4][$j] : $m2[3][$j];
}
}
print_r($list);//查看结果变量
?>
Copy after login

输出结果如下:

---------- php ----------
Array
(
    [0] => Array
        (
            [title] => 小桔灯分类信息网
            [alt] => 小桔灯分类信息网
            [onload] => ResizeImage(this,860)
            [src] => http://www.xiaojudeng.com/uploadfile/2011/0910/20110910102454887.jpg
        )
    [1] => Array
        (
            [title] => 小桔灯分类信息网
            [alt] => 小桔灯分类信息网
            [onload] => ResizeImage(this,860)
            [src] => http://www.xiaojudeng.com/uploadfile/2011/0910/20110910102455105.jpg
        )
    [2] => Array
        (
            [title] => 小桔灯分类信息网
            [alt] => 小桔灯分类信息网
            [onload] => ResizeImage(this,860)
            [src] => http://www.xiaojudeng.com/uploadfile/2011/0910/20110910102459367.jpg
        )
)
输出完毕 (耗时 0 秒) - 正常终止
Copy after login

下面是另外一个写法的,充分证明此正则方法可以完美匹配img标签的各属性:

<?php
/*
create by tuzwu@qq.com for http://www.xiaojudeng.com
*/
$str = <<<EOT
<img src = "http://www.xiaojudeng.com/uploadfile/2011/0910/20110910100916470.jpg" class =&#39;image x1&#39; alt="小桔灯分类信息网" shuxing =shux />
<img src = "http://www.xiaojudeng.com/uploadfile/2011/0910/20110910100916803.jpg" class =&#39;image x2&#39; alt=&#39;小桔灯分类信息网&#39; title=abc shuxing =shux />
这里是小桔灯分类信息网 http://www.xiaojudeng.com
<a href="http://www.xiaojudeng.com/" class="a" alt=abc shuxing="shux" />只取得img标签
EOT;
$list = array();//这里存放结果map
$c1 = preg_match_all(&#39;/<img\s.*?>/&#39;, $str, $m1);//先取出所有img标签文本
for($i=0; $i<$c1; $i++) {//对所有的img标签进行取属性
$c2 = preg_match_all(&#39;/(\w+)\s*=\s*(?:(?:(["\&#39;])(.*?)(?=\2))|([^\/\s]*))/&#39;, $m1[0][$i], $m2);//匹配出所有的属性
for($j=0; $j<$c2; $j++) {//将匹配完的结果进行结构重组
$list[$i][$m2[1][$j]] = !empty($m2[4][$j]) ? $m2[4][$j] : $m2[3][$j];
}
}
print_r($list);//查看结果变量
?>
Copy after login

输出结果如下:

---------- php ----------
Array
(
    [0] => Array
        (
            [src] => http://www.xiaojudeng.com/uploadfile/2011/0910/20110910100916470.jpg
            [class] => image x1
            [alt] => 小桔灯分类信息网
            [shuxing] => shux
        )
    [1] => Array
        (
            [src] => http://www.xiaojudeng.com/uploadfile/2011/0910/20110910100916803.jpg
            [class] => image x2
            [alt] => 小桔灯分类信息网
            [title] => abc
            [shuxing] => shux
        )
)
输出完毕 (耗时 0 秒) - 正常终止
Copy after login


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