首頁 >php教程 >php手册 >php正则提取img所有属性值,php提取img属性值

php正则提取img所有属性值,php提取img属性值

WBOY
WBOY原創
2016-06-13 08:53:34909瀏覽

php正则提取img所有属性值,php提取img属性值

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

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn