Home > Backend Development > PHP Tutorial > PHP regular extracts all attribute values ​​​​of img, PHP extracts img attribute values_PHP tutorial

PHP regular extracts all attribute values ​​​​of img, PHP extracts img attribute values_PHP tutorial

WBOY
Release: 2016-07-12 09:07:44
Original
1059 people have browsed it

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>
Copy after login

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1059462.htmlTechArticlephp正则提取img所有属性值,php提取img属性值 $ext = 'gif|jpg|jpeg|bmp|png'; // 罗列图片后缀从而实现多扩展名匹配 by http://www.k686.com 绿色软件 $st...
Related labels:
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