php正則替換img標記屬性的方法是:可以通過preg_match()函數(shù)來實現(xiàn)。該函數(shù)用來執(zhí)行一個正則表達式匹配,并返回匹配次數(shù)。preg_match()函數(shù)在第一次匹配后就會停止搜索。
我們可以通過preg_match()函數(shù)來實現(xiàn)。
(推薦教程:php教程)
函數(shù)介紹:
preg_match 函數(shù)用于執(zhí)行一個正則表達式匹配。
函數(shù)語法:
int preg_match( string $pattern ,string $subject [,array &$matches [,int $flags = 0 [,int $offset = 0]]] )
返回值:
返回 pattern 的匹配次數(shù)。 它的值將是 0 次(不匹配)或 1 次,因為 preg_match() 在第一次匹配后 將會停止搜索。preg_match_all() 不同于此,它會一直搜索subject 直到到達結(jié)尾。 如果發(fā)生錯誤preg_match()返回 FALSE。
代碼實現(xiàn):
<?php /*PHP正則提取圖片img標記中的任意屬性*/ $str = '<center><img src="/uploads/images/20100516000.jpg" height="120" width="120"><br />PHP正則提取或更改圖片img標記中的任意屬性</center>';