Heim >Backend-Entwicklung >PHP-Tutorial >php正则表达式匹配img中任意属性的方法

php正则表达式匹配img中任意属性的方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-23 13:17:311480Durchsuche

`<?php /*PHP正则提取图片img标记中的任意属性*/ $str = '<center><img  src="/static/imghwm/default1.png"  data-src="/uploads/images/20100516000.jpg"  class="lazy"    style="max-width:90%"  style="max-width:90%" alt="php正则表达式匹配img中任意属性的方法" ><br />PHP正则提取或更改图片img标记中的任意属性</center>'; //1、取整个图片代码 preg_match('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i',$str,$match); echo $match[0]; //2、取width preg_match('/<img  src="/static/imghwm/default1.png"  data-src="/uploads/images/20100516000.jpg"  class="lazy" .+(   style="max-width:90%"?\d*\"?).+ alt="php正则表达式匹配img中任意属性的方法" >/i',$str,$match); echo $match[1]; //3、取height preg_match('/<img  src="/static/imghwm/default1.png"  data-src="/uploads/images/20100516000.jpg"  class="lazy" .+(height=\"?\d*\"?).+ alt="php正则表达式匹配img中任意属性的方法" >/i',$str,$match); echo $match[1]; //4、取src preg_match('/<img  src="/static/imghwm/default1.png"  data-src="/uploads/images/20100516000.jpg"  class="lazy" .+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+ alt="php正则表达式匹配img中任意属性的方法" >/i',$str,$match); echo $match[1]; (PS:T不错的php Q扣峮:276167802,验证:csl) /*PHP正则替换图片img标记中的任意属性*/ //1、将替换为) print preg_replace('/(<img  src="/static/imghwm/default1.png"  data-src="/uploads/images/20100516000.jpg"  class="lazy" .+src=\"?.+)(images\/)(.+\.(jpg|gif|bmp|bnp|png)\"?.+ alt="php正则表达式匹配img中任意属性的方法" >)/i',"\${1}uc/images/\${3}",$str); echo "<hr/>"; //2、将src="/uploads/images/20100516000.jpg"替换为src="/uploads/uc/images/20100516000.jpg",并省去宽和高 print preg_replace('/(<img ).+(src=\"?.+)images\/(.+\.(jpg|gif|bmp|bnp|png)\"?).+ alt="php正则表达式匹配img中任意属性的方法" >/i',"\${1} \${2}uc/images/\${3}>",$str); ?> `

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn