【问题标题】:Hyperlink Youtube to Embed Code超链接 Youtube 以嵌入代码
【发布时间】:2011-06-10 10:38:14
【问题描述】:

我在使用 Preg_replace 和 preg_match_all 将 Youtube URL 转换为嵌入代码时遇到了一些麻烦。是的,我知道这个话题已经在 stackoverflow 中涉及到了,但并不完全符合我的要求。

我可以从没有 html 的 url 获取 ID:

http://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)(\w*)(&(amp;)?[\w\?=]*)?

但我的网址格式如下:

<a href="http://www.youtube.com/watch?v=C9KAqhbIZ7o" class="comment-link">http://www.youtube.com/watch?v=C9KAqhbIZ7o</a>

我想把它全部转换成这个:

<object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&amp;hl=es_ES&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&amp;hl=es_ES&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object>

有人可以施展魔法并告诉我正确的表达式来检测所有 url,获取一次 ID 并将所有内容转换为嵌入代码吗?提前非常感谢您!

更新信息:

为了帮助并使其更简洁......

我有这个:

<p>This is an example of comment</p><strong>Hi bold!</strong><p>Look a youtube url! <a href="http://www.youtube.com/watch?v=C9KAqhbIZ7o" class="comment-link">http://www.youtube.com/watch?v=C9KAqhbIZ7o</a></p>

我想得到这个:

<p>This is an example of comment</p><strong>Hi bold!</strong><p>Look a youtube url! <object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&amp;hl=es_ES&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&amp;hl=es_ES&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object></p>

感谢大家的帮助,非常感谢!

【问题讨论】:

  • 您如何获得“未格式化的 url”?因为获取 a 元素的 href 参数更快。
  • 感谢您的评论。 URL es 会自动格式化,因为 Wordpress 会对其进行转换,并且我想保持它以这种方式运行。是的,我知道更快,但我想替换所有 a 元素,而不仅仅是获取视频的 ID。

标签: php wordpress preg-replace preg-match preg-match-all


【解决方案1】:

将 $html 替换为需要解析的 html 字符串。

$html=<<<HTML
<p>This is an example of comment</p><strong>Hi bold!</strong><p>Look a youtube url! <a href="http://www.youtube.com/watch?v=C9KAqhbIZ7o" class="comment-link">http://www.youtube.com/watch?v=C9KAqhbIZ7o</a></p>

HTML;


$regex="/v\=([\-\w]+)/";

preg_match_all($regex,$html,$out);

$out[1]=array_unique($out[1]);

foreach($out[1] as $o){

        $reg="/(<a).*(youtube.com).*($o).*(\/a>)/";

        $embed= <<<HTML
        <object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/$o=1&amp;hl=es_ES&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$o?fs=1&amp;hl=es_ES&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object>
HTML;

        $html=preg_replace($reg,$embed, $html);

}

echo $html;

【讨论】:

  • 非常感谢!! :) 它没有按预期工作,因为我需要保存所有评论文本,只用嵌入代码替换视频的 url 并替换 url 一次(我已经测试过它并转换了两次)。感谢您的努力!!!
  • 完美运行!!!!!! :) 谢谢谢谢谢谢谢谢!!你不知道我为此花了多少时间。谢谢!!!
【解决方案2】:

我用这个代码

        // url of video
    $url = $row['url'];
    $id=0;
    // we get the unique video id from the url by matching the pattern
    preg_match("/v=([^&]+)/i", $url, $matches);
    if(isset($matches[1])) $id = $matches[1];
    if(!$id) {
        $matches = explode('/', $url);
        $id = $matches[count($matches)-1];
    }
    // this is your template for generating embed codes
    $code = '<div id="img_wrapper"><object width="640" height="458"><param name="movie" value="http://www.youtube.com/v/{id}&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/{id}&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></div>';

    // we replace each {id} with the actual ID of the video to get embed code for this particular video
    $code = str_replace('{id}', $id, $code);

    echo $code;

【讨论】:

  • 嗯,它可以工作,所以我现在只需要得到一个 preg_match 就可以得到 youtube 的所有 url,这是我的大问题!!为了更明确,我已经更新了请求!谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-02-12
  • 2021-03-13
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 2013-04-08
  • 1970-01-01
相关资源
最近更新 更多