2013-01-09 76 views
0

在WordPress中我使用的是相同的YouTube嵌入代碼,我總是有,但現在它的返回和未定義的錯誤抵消:1 - 我注意到它也適用於在代碼行。PHP錯誤嵌入

function embed_youtube_video($post_id) { 
    $share_url = get_field('share_url'); 
    preg_match('/youtu\.be\/(.+)$/', $share_url, $matches); 
    // the next line throws the error 
    if($matches[1]) { 
     return '<iframe width="640" height="360" src="http://www.youtube.com/embed/' . $matches[1] . '?rel=0&wmode=transparent" frameborder="0" allowfullscreen></iframe>'; 
    } else { 
     return false; 
    } 
} 

現在我不是一個PHP專家,偷了這些代碼,舊的網站,所以我可能已經做了一些超級簡單修復,只是沒有察覺它。預先感謝您的幫助。

+0

什麼是你的錯誤,並在其中線是什麼呢? – samayo

回答

1

你得到的錯誤,因爲沒有比賽,因此$matches[1]不存在。

保持代碼的流程相同的行更改爲:

if(isset($matches[1])) { 

我應該提到,isset()檢查,看是否有變量定義。在這種情況下,我們檢查以確保定義了$matches[1]。它可以完成與您現有生產線相同的任務而不會產生錯誤,因爲isset()不受未定義錯誤的影響,因爲它的功能本質。

+0

謝謝你,對不起,我很晚回到你身邊,但這讓我很好! –