WordPress如何讓您通過url嵌入youtube/dailymotion/vimeo視頻?例如,如果您輸入[youtube = http://www.youtube.com/watch?v = cXXm696UbKY],視頻就會出現在那裏。 反正有沒有在php中使用markdown安全地做到這一點?允許將視頻嵌入評論/文字
0
A
回答
2
大多數(所有?)的這些視頻平臺都提供了oEmbed支持。
例如對於YouTube視頻http://www.youtube.com/watch?v=cXXm696UbKY
它是http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch%3Fv%cXXm696UbKY
。
這將返回一個您可以用json_decode
輕鬆解析的響應。
{
"provider_url": "http:\/\/www.youtube.com\/",
"title": "Auto-Tune the News #8: dragons. geese. Michael Vick. (ft. T-Pain)",
"html": "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/bDOYN-6gdRE?fs=1\"><\/param><param name=\"allowFullScreen\" value=\"true\"><\/param><param name=\"allowscriptaccess\" value=\"always\"><\/param><embed src=\"http:\/\/www.youtube.com\/v\/bDOYN-6gdRE?fs=1\" type=\"application\/x-shockwave-flash\" width=\"425\" height=\"344\" allowscriptaccess=\"always\" allowfullscreen=\"true\"><\/embed><\/object>",
"author_name": "schmoyoho",
"height": 344,
"thumbnail_width": 480,
"width": 425,
"version": "1.0",
"author_url": "http:\/\/www.youtube.com\/user\/schmoyoho",
"provider_name": "YouTube",
"thumbnail_url": "http:\/\/i3.ytimg.com\/vi\/bDOYN-6gdRE\/hqdefault.jpg",
"type": "video",
"thumbnail_height": 360
}
有趣的部分是html
屬性。
因此,我們所要做的就是搜索[YouTube=...]
標籤的文本,提取YouTube網址並通過oEmbed檢索嵌入代碼。
這裏工作的例子:
<?php
function getYouTubeCode($url)
{
$oembedUrl = 'http://www.youtube.com/oembed?url=' . urlencode($url);
// The @-operator suppresses errors if the YouTube oEmbed service can't handle our request
$data = @file_get_contents($oembedUrl);
// If $data contains invalid JSON code, it will return null
$data = json_decode($data);
// So if $data is not an object now, we abort
if (!is_object($data)) {
return '';
}
// Otherwise we return the YouTube embed code
return $data->html;
}
$text = '<h1>Hi There</h1><p>You gotta watch this video:</p><p>[YouTube=http://www.youtube.com/watch?v=cXXm696UbKY]</p>';
$matches = array();
// We scan the $text variable for all occurrences of "[YouTube=<Any character except right squared bracket>]"
if (preg_match_all('/\[YouTube=([^\]]+)\]/i', $text, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
// Eg. $match[0] is "[YouTube=http://www.youtube.com/watch?v=cXXm696UbKY]"
// and $match[1] is "http://www.youtube.com/watch?v=cXXm696UbKY"
$text = str_replace($match[0], getYouTubeCode($match[1]), $text);
}
}
echo $text;
1
我真的不知道WordPress的問題,但基本的邏輯是搜索網址,並將其轉換爲Youtube嵌入代碼,添加它周圍的東西!我認爲preg_replace()
是你必須牢記的!
相關問題
- 1. 只允許視頻嵌入代碼(Rails)
- 2. php strip_tags允許評論
- 3. 在評論或消息中嵌入youtube或vimeo視頻,如Facebook
- 4. 訪問視頻評論
- 5. 使用Django時出現「評論帖子不允許(400)」評論
- 6. Facebook的評論不允許發表匿名評論
- 7. 評論模塊:允許匿名用戶回覆評論
- 8. 只允許評論的所有者刪除他們的評論
- 9. VBScript,HTA - INI分析,允許內嵌評論
- 10. 檢查是否允許使用jQuery視頻嵌入
- 11. 檢查,是否允許嵌入(YouTube視頻)PHP,新的YouTube API
- 12. 允許用戶嵌入視頻的主要安全問題
- 13. Youtube嵌入的視頻允許由管理員更改
- 14. Oracle:更新只允許評論
- 15. 的Drupal允許匿名評論
- 16. wordpress只允許添加評論
- 17. 允許在播放JSON評論
- 18. Wordpress wp_new_comment允許多條評論
- 19. 治癒'字符串「 - 」在評論中不允許。「例外?
- 20. 嵌入視頻
- 21. 如何在停用「允許評論」後襬脫Wordpressa中的「發表評論」文字?
- 22. 如何構建允許自動評論每篇文章的JSP?
- 23. Mongoid:評論 字段:從嵌入的文檔上的父
- 24. Facebook的評論插件 - 允許用戶與會話刪除評論
- 25. YouTube API視頻條目數量評論
- 26. Youtube - 添加評論以迴應視頻
- 27. 的Youtube API - 視頻評論分頁
- 28. 應用評論視頻(App Store)
- 29. 不應允許上傳受感染的文件/視頻。即使允許上傳文件/視頻類型(.mp4)
- 30. 嵌入YouTube視頻
是錯在愛上一個答案? – jonnnnnnnnnie 2010-12-05 00:45:55