以下正則表達式獲取所有圖像src,但在開始時顯示src="
,在路徑末尾顯示"
。簡單的preg_match問題
我該如何刪除它並只顯示路徑?
preg_match('/src="(.*?)"/i' , $content1 , $matches);
以下正則表達式獲取所有圖像src,但在開始時顯示src="
,在路徑末尾顯示"
。簡單的preg_match問題
我該如何刪除它並只顯示路徑?
preg_match('/src="(.*?)"/i' , $content1 , $matches);
代碼:
$re = '/(?<=src=")(?:.*?)(?=")/ui';
$txt = '<img src="http://www.gravatar.com/avatar/6f5de9?s=32&d=identicon&r=PG" height="32" width="32" alt="">';
$nMatches = preg_match($re, $txt, $aMatches);
輸出:
Array
(
[0] => http://www.gravatar.com/avatar/6f5de9?s=32&d=identicon&r=PG
)
你幾乎做得對。正則表達式是正確的,但是:
echo $matches[1];
這將輸出第一個捕獲的子模式。第一場比賽($matches[0]
)將始終包含匹配的完整文本,即包含src=""
比特。
$匹配將包含第一個元素中的整個匹配模式,然後是每個子模式的元素。在這種情況下,你需要$ matches [1]作爲你想要的東西。
只是呼應'$匹配[1]' – Starx 2011-03-29 12:26:36