<?php
$str
=
'<center><img src="/uploads/images/20100516000.jpg" height="120" width="120"><br />PHP正则提取或更改图片img标记中的任意属性</center>'
;
preg_match(
'/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i'
,
$str
,
$match
);
echo
$match
[0];
preg_match(
'/<img.+(width=\"?\d*\"?).+>/i'
,
$str
,
$match
);
echo
$match
[1];
preg_match(
'/<img.+(height=\"?\d*\"?).+>/i'
,
$str
,
$match
);
echo
$match
[1];
preg_match(
'/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i'
,
$str
,
$match
);
echo
$match
[1];
print
preg_replace(
'/(<img.+src=\"?.+)(images\/)(.+\.(jpg|gif|bmp|bnp|png)\"?.+>)/i'
,"\${1}uc/images/\${3}",
$str
);
echo
"<hr/>"
;
print
preg_replace(
'/(<img).+(src=\"?.+)images\/(.+\.(jpg|gif|bmp|bnp|png)\"?).+>/i'
,"\${1} \${2}uc/images/\${3}>",
$str
);
?>