如何用该URL中的参数值替换url?

时间:2012-03-09 07:07:21

标签: php html regex url preg-replace

像:

$output = preg_replace( 
    "/http\:\/\/(.*)\?mid\=([0-9|a-z|A-Z]*)/ismU", 
    "<iframe src=\"http://professional.player.qbrick.com/player.aspx?mid=${2}\"></iframe>", 
    $output 
);

这不起作用......

我想要的是替换文本中的任何网址,如

http://professional.player.qbrick.com/player.aspx?mid=asdas23234ASD

<iframe src="http://professional.player.qbrick.com/player.aspx?mid=asdas23234ASD"></iframe>

2 个答案:

答案 0 :(得分:0)

您可以使用sprintf来构建字符串。

http://php.net/manual/en/function.sprintf.php

答案 1 :(得分:0)

怎么样:

$output = 'http://professional.player.qbrick.com/player.aspx?mid=asdas23234ASD';
$output = preg_replace( 
    "#(http://.*?\?mid=[0-9a-zA-Z]+)\b#ismU", 
    "<iframe src=\"$1\"></iframe>", 
    $output 
);
echo $output,"\n";

<强>输出:

<iframe src="http://professional.player.qbrick.com/player.aspx?mid=asdas23234ASD"></iframe>