PHP_正则_获取图片所有属性
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
<?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 );
echo $match [1];
?
//4、取src
preg_match( '/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i' , $str , $match );
echo $match [1];
?
/*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 );
?
?>
轉載于:https://my.oschina.net/miaowang/blog/304953
總結
以上是生活随笔為你收集整理的PHP_正则_获取图片所有属性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS 6.4下操作kdump执行
- 下一篇: ActiveReports 9实战教程(