PHP - 为什么preg_replace不工作?

时间:2011-03-02 05:26:06

标签: php preg-replace

我正在尝试替换[picasa和],但以下代码段无效。

$vars = '[picasa flashvars="host=picasaweb.google.com&hl=en_US&feat=flashalbum&RGB=0x000000&feed=https%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2F100016363917755589270%2Falbumid%2F5578059546640576577%3Falt%3Drss%26kind%3Dphoto%26hl%3Den_US" width="288" height="192"]';

$patterns[0] =  "/\[picasa\] /";
$patterns[1] = "/\]/";

$replacements[0] = "<embed type='application/x-shockwave-flash' src='https://picasaweb.google.com/s/c/bin/slideshow.swf'";
$replacements[1] = " pluginspage='http://www.macromedia.com/go/getflashplayer'>";


$match = preg_replace($patterns,$replacements,$vars);
echo $match;

我知道我可以使用str_replace,但我想改用preg_replace。

我在这里做错了吗?

1 个答案:

答案 0 :(得分:2)

$patterns[0]匹配[picasa]

中不存在的$vars

应该是:

preg_replace('/\[picasa\s*([^\]]+)\]/',$replacements[0].'$1'.$replacements[1],$vars);