<?php
/*PHP正則提取圖片img標記中的任意屬性*/
$str = <center><img src="/uploads/images/jpg" height="" width=""><br />PHP正則提取或更改圖片img標記中的任意屬性</center>;
//取整個圖片代碼
preg_match(/<s*imgs+[^>]*?srcs*=s*(|")(*?)[^>]*?/?s*>/i$str$match);
echo $match[];
//取width
preg_match(/<img+(width="?d*"?)+>/i$str$match);
echo $match[];
//取height
preg_match(/<img+(height="?d*"?)+>/i$str$match);
echo $match[];
//取src
preg_match(/<img+src="?(+(jpg|gif|bmp|bnp|png))"?+>/i$str$match);
echo $match[];
/*PHP正則替換圖片img標記中的任意屬性*/
//將src="/uploads/images/jpg"替換為src="/uploads/uc/images/jpg")
print preg_replace(/(<img+src="?+)(images/)(+(jpg|gif|bmp|bnp|png)"?+>)/i"${}uc/images/${}"$str);
echo "<hr/>";
//將src="/uploads/images/jpg"替換為src="/uploads/uc/images/jpg"並省去寬和高
print preg_replace(/(<img)+(src="?+)images/(+(jpg|gif|bmp|bnp|png)"?)+>/i"${} ${}uc/images/${}>"$str);
?>
From:http://tw.wingwit.com/Article/program/PHP/201311/20870.html