在我的文本中替换youtube url以嵌入

时间:2015-03-18 14:58:12

标签: php

尝试在我的文本中替换youtube网址以嵌入。我做对了,除了我还要打印文本" https://www.youtube.com"在嵌入之前。 我该怎么做才能改变?

return preg_replace(
    '#(http://(www.)?youtube.com)?/(v/|watch\?v\=)([-|~_0-9A-Za-z]+)&?.*?#i',
    '<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/$4" frameborder="0" allowfullscreen></iframe>',
    $text
);

1 个答案:

答案 0 :(得分:0)

尝试使用此正则表达式:

return preg_replace(
    '/((http(s)?:\/\/)?(www.)?youtube.com\/watch\?v=.*)/i',
    '<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/$4" frameborder="0" allowfullscreen></iframe>',
    $text
);
相关问题