2011-03-31 26 views
0

我正嘗試使用preg_match從嵌入代碼中提取YouTube視頻的寬度。任何人都可以用正則表達式來幫助我嗎?

的代碼是這樣的:

<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/3uw-nUvGrBY" frameborder="0" allowfullscreen></iframe> 

,我想提取寬度= 「480」

我試着: /寬度= 「[0-9] +」/ /width = \「[0-9] + \」/ /width=.[0-9]+./

但它們都不起作用。如果刪除了最後的雙引號,並且數字,我可以提取寬度=」但我不能超出。

誰能幫助?

回答

0
$string = '<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/3uw-nUvGrBY" frameborder="0" allowfullscreen></iframe>'; 
preg_match('/.*width="(\d+)".*/', $string, $match);\ 
echo $match[1]; 

但我敢肯定的是,PHP或者JS DOM能夠用更少的痛苦做...

+0

我將如何去使用DOM? – Sharon 2011-04-01 10:07:00

0

您的實際來源可能以不同的格式你的榜樣HTML片段看起來太示範試試這個:。

preg_match('#\b(width\s*=)\s*["\']?(\d+)#i', $source, $match); 
print $match[2]; 
0
$ptn = "/(?<=width=\")\d*(?=\")/"; 
$str = '<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/3uw-nUvGrBY" frameborder="0" allowfullscreen></iframe>'; 
preg_match($ptn, $str, $matches); 
echo $matches[0]; // 480 
相關問題