<?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);
?>