`<?phpphp
/PHP正則提取圖片img標記中的任意屬性/ $str = '<center><img src="/uploads/images/20100516000.jpg" height="120" width="120"><br />PHP正則提取或更改圖片img標記中的任意屬性</center>';圖片
//一、取整個圖片代碼 preg_match('/<\simg\s+[^>]?src\s*=\s*('|")(.?)\1[^>]?/?\s*>/i',$str,$match); echo $match[0];im
//二、取width preg_match('/<img.+(width="?\d*"?).+>/i',$str,$match); echo $match[1];img
//三、取height preg_match('/<img.+(height="?\d*"?).+>/i',$str,$match); echo $match[1];upload
//四、取src preg_match('/<img.+src="?(.+.(jpg|gif|bmp|bnp|png))"?.+>/i',$str,$match); echo $match[1]; (PS:T不錯的php Q扣峮:276167802,驗證:csl)gif
/PHP正則替換圖片img標記中的任意屬性/ //一、將src="/uploads/images/20100516000.jpg"替換爲src="/uploads/uc/images/20100516000.jpg") print preg_replace('/(<img.+src="?.+)(images/)(.+.(jpg|gif|bmp|bnp|png)"?.+>)/i',"${1}uc/images/${3}",$str); echo "<hr/>";
//二、將src="/uploads/images/20100516000.jpg"替換爲src="/uploads/uc/images/20100516000.jpg",並省去寬和高 print preg_replace('/(<img).+(src="?.+)images/(.+.(jpg|gif|bmp|bnp|png)"?).+>/i',"${1} ${2}uc/images/${3}>",$str);
?> `