Home  >  Article  >  php教程  >  php正则提取img所有属性值,php提取img属性值

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

WBOY
WBOYOriginal
2016-06-13 08:53:34891browse

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>

 

Statement:
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