天天看點

PHP正則擷取圖檔資訊

<?php

/*php正則提取圖檔img标記中的任意屬性*/

$str = '<center><img src="/uploads/images/20100516000.jpg" height="120" width="120"><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.+(width=\"?\d*\"?).+>/i',$str,$match);

echo $match[1];

//3、取height

preg_match('/<img.+(height=\"?\d*\"?).+>/i',$str,$match);

//4、取src

preg_match('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i',$str,$match);

echo $match[1]; (ps:t不錯的php q扣峮:276167802,驗證:csl)

/*php正則替換圖檔img标記中的任意屬性*/

//1、将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/>";

//2、将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);

?>

下一篇: PHP時間