2011-11-05 135 views
0

我有以下代碼:str_replace與隨機值?

$embed_url = str_replace('<iframe width="512" height="288" frameborder="0" scrolling="no" src="', '', $embed_url); 
    $embed_url = str_replace('"></iframe>', '', $embed_url); 

會發生什麼事是iframe的寬度和高度是不固定的值,因此它們可以是任何其他數字。

我怎樣才能使此作品的任何寬度/高度?

謝謝。

編輯:我想要得到的是src值。

回答

2

你可以使用正則表達式:如果你想要的是檢索的src

$embed_url = preg_replace('/<iframe width=".*?" height=".*?" frameborder="0" scrolling="no" src="/', '', $embed_url); 

,你可以使用preg_match()代替:

if (preg_match('/<iframe.*?src="(.+?)"/ms', $embed_url, $matches)) { 
    $src = $matches[1]; 
} 
+0

Tha是完美的。泰! – PGZ

1

你會使用

$embed_url = preg_replace('!<iframe width="[0-9]+?" height="[0-9]+?" frameborder="0" scrolling="no" src="!','',$embed_content); 
0

代替使用str_replace,只需使用bu以價值取代爲例:

$width = '512'; 
$height = '288'; 

$iframe = '<iframe width="$width" height="$height" frameborder="0" scrolling="no" src="$embed_url"></iframe>'; 

HTH。