Home > Backend Development > PHP Tutorial > php正则提取img全部属性值

php正则提取img全部属性值

WBOY
Release: 2016-06-13 12:25:44
Original
798 people have browsed it

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

 

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