带有随机值的str_replace?

时间:2011-11-05 20:24:48

标签: php 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值。

3 个答案:

答案 0 :(得分:2)

您可以使用正则表达式:

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

如果您只想检索src的值,则可以改为使用preg_match()

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

答案 1 :(得分:1)

你会用

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

答案 2 :(得分:0)

而不是使用str_replace,只需使用内置值替换,即:

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

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

HTH。