到目前为止,这是我得到的:

<?php

 $content = "word1 http://www.youtube.com/watch?v=yqfKe-67foQ&feature=related word2 http://www.youtube.com/watch?v=2vq7gDEn99Y&feature=related word3 http://www.youtube.com/watch?v=nW5HxgMYRto\nhttp://www.youtube.com/watch?v=8Uc2lpH0iZ0&feature=fvhl";
 $pattern = '/http:\/\/www\.youtube\.com\/watch\?(.*)v=([a-zA-Z0-9_\-]+)(\S*)/i';
 $replace = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/$2&amp;hl=en_US&amp;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$2&amp;hl=en_US&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>';

 $content = preg_replace($pattern, $replace, $content);
 echo $content;

?>

老实说,我不知道为什么它不起作用。一些帮助,将不胜感激。谢谢。

最佳答案

您只需要使*不贪心即可:

http:\/\/www\.youtube\.com\/watch\?(.*?)v=([a-zA-Z0-9_\-]+)(\S*)

参见"Watch out for greediness"

关于php - 如何用嵌入式代码(在PHP中)替换文本中的Youtube网址?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3245037/

10-10 08:28