首页 >后端开发 >php教程 >php正则提取img全部属性值

php正则提取img全部属性值

WBOY
WBOY原创
2016-06-13 12:25:44794浏览

php正则提取img所有属性值

<span style="color: #800080;">$ext</span> = 'gif|jpg|jpeg|bmp|png';<span style="color: #008000;">//</span><span style="color: #008000;">罗列图片后缀从而实现多扩展名匹配 by http://www.k686.com 绿色软件  </span><span style="color: #800080;">$str</span> = ''<span style="color: #000000;">;    </span><span style="color: #800080;">$list</span> = <span style="color: #0000ff;">array</span>();    <span style="color: #008000;">//</span><span style="color: #008000;">这里存放结果map  </span><span style="color: #800080;">$c1</span> = <span style="color: #008080;">preg_match_all</span>('/php正则提取img所有属性值/', <span style="color: #800080;">$str</span>, <span style="color: #800080;">$m1</span>);  <span style="color: #008000;">//</span><span style="color: #008000;">先取出所有img标签文本  </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0; <span style="color: #800080;">$i</span>$c1; <span style="color: #800080;">$i</span>++) {    <span style="color: #008000;">//</span><span style="color: #008000;">对所有的img标签进行取属性  </span>    <span style="color: #800080;">$c2</span> = <span style="color: #008080;">preg_match_all</span>('/(\w+)\s*=\s*(?:(?:(["\'])(.*?)(?=\2))|([^\/\s]*))/', <span style="color: #800080;">$m1</span>[0][<span style="color: #800080;">$i</span>], <span style="color: #800080;">$m2</span>);   <span style="color: #008000;">//</span><span style="color: #008000;">匹配出所有的属性  </span>    <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$j</span>=0; <span style="color: #800080;">$j</span>$c2; <span style="color: #800080;">$j</span>++) {    <span style="color: #008000;">//</span><span style="color: #008000;">将匹配完的结果进行结构重组  </span>        <span style="color: #800080;">$list</span>[<span style="color: #800080;">$i</span>][<span style="color: #800080;">$m2</span>[1][<span style="color: #800080;">$j</span>]] = !<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$m2</span>[4][<span style="color: #800080;">$j</span>]) ? <span style="color: #800080;">$m2</span>[4][<span style="color: #800080;">$j</span>] : <span style="color: #800080;">$m2</span>[3][<span style="color: #800080;">$j</span><span style="color: #000000;">];      }  }  </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$list</span>); <span style="color: #008000;">//</span><span style="color: #008000;">查看结果变量</span>

 

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn