Maison >développement back-end >tutoriel php >php正则提取img全部属性值
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>