【问题标题】:How to replace an url with the value of a parameter inside that URL?如何用该 URL 中的参数值替换 url?
【发布时间】:2012-03-09 07:07:21
【问题描述】:

喜欢:

$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 
);

这不起作用...

我想要的是替换文本中的任何 URL,例如

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

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

【问题讨论】:

  • 您是否需要查找或识别文本中的 URL 开头?

标签: php html regex url preg-replace


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    怎么样:

    $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>
    

    【讨论】:

      猜你喜欢
      • 2011-07-31
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多