2012-04-27 3 views
0

다음 코드를 사용하여 링크를 삽입하여 비디오로 표시하려면 YouTube URL을 바꿉니다. ? 그러나 나는 V 이외의 더 많은 매개 변수를 가진 링크에서 동영상을 볼 수없는 문제 = video_code에 온embed youtube 비디오 매개 변수가 무시되었습니다.

PHP 코드 :

function embedYoutube($text) 
{ 

    $pattern = '|http://www\.youtube\.com/watch\?.*?\bv=([^ ]+)|'; 
    $replacement = '<div style="width:100%;float:left;margin-top:15px;margin-bottom:15px;"><iframe width="570" height="315" src=http://www.youtube.com/embed/$1 frameborder="0" allowfullscreen></iframe></div>'; 
    return preg_replace($pattern, $replacement, $text, 1); 

} 

답변

0

업데이트에만 v 매개 변수를 일치하도록 정규식하고, 첫 번째 전에 & :

|http://www\.youtube\.com/watch\?.*?\bv=([^ ]+)\&?| 
+0

URL에 "&"가 없으면 어떻게해야합니까? meining just/watch? v = abcd – fxuser

+0

http://www.youtube.com/watch?v=1Vo_F_2eWMw&feature=g를 사용하여 내 답변을 업데이트했습니다 (끝에 '?'추가) – j0k

+0

새 패턴으로 업데이트했습니다. -all-u "오류가 발생했습니다. bla bla bla"를 반환하지만, http://www.youtube.com/watch?v=1Vo_F_2eWMw를 사용하면 제대로 작동합니다 ... – fxuser