Heim > Artikel > Backend-Entwicklung > php正则提取img所有属性值,php提取img属性值_PHP教程
<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>