【发布时间】:2017-01-30 13:14:52
【问题描述】:
我网站中的帖子包含来自以下任何嵌入的视频(单个)。
- YouTube
- 脸书
我的问题是,在前端获取它们时,我想知道我的内容是否有嵌入,如果有,则嵌入以下哪个。 (iframe 存在检查是一种(肮脏的)方式,它仍然可以为 instagram 工作)
PHP代码:
$video_start = strpos($singlePost->post_content, "<iframe");//Get to the start of the iframe(video)
$video_stop = strpos($singlePost->post_content, "</iframe>");//Get to the end of the iframe(video)
$iframe_content = substr($singlePost->post_content, $video_start, $video_stop);
$xpath = new DOMXPath(@DOMDocument::loadHTML($iframe_content));
$iframe_src = $xpath->evaluate("string(//iframe/@src)");
$parsed_url = parse_url($iframe_src);
$host = $parsed_url['host'];
if(strpos($host, "youtube") !== false) { // If it is a youtube video append this
$iframe_src = $iframe_src."?rel=0";// This option has to be appended of youtube URL's
$related_social_icon = "youtube";
$related_social_media = "youtube";
}
<iframe class="<?php echo $iframe_class; ?>" src="<?php echo $iframe_src; ?>" style="background-size: cover;" allowfullscreen></iframe>
以上代码适用于 youtube,但不适用于 instagram,因为当插入 instagram 作为块引用标签时,如果您回显它们,由于其中的脚本,它将立即成为 iframe 标签。
【问题讨论】:
-
先生们,感谢这个负面的礼物,请告诉我是什么让我的帖子配得上这个礼物。我不认为这方面存在问题。
-
老实说这个问题太狭窄了。您尝试过的事情没有任何努力的迹象或发布的代码。这些东西就是 stackoverflow 的用途。
标签: php wordpress facebook youtube instagram