gpt4 book ai didi

php - 将 Youtube 链接添加到 php 循环中的 slider

转载 作者:行者123 更新时间:2023-11-28 02:04:48 25 4
gpt4 key购买 nike

我在网站上有一个 slider 。 slider 称为任何 slider 。

您可以在下面的链接上查看它:

http://css-tricks.com/examples/AnythingSlider/#&panel1-1

slider 允许嵌入 YouTube 视频。

但是目前我有一个 php 外观,它循环遍历一个目录以显示上传到该目录中的任何横幅图像。

我不确定如何将 youtube 链接添加为 slider 之一,但仍然在目录中保持循环。

这是我使用的代码:

<ul id="slider">

<?php
if ($handle = opendir('public/slider/'))
{
while (false !== ($file = readdir($handle)))
{
$files[] = $file;
}

$files = array_slice($files, 2);

foreach ($files as $file)
{
$name = explode('.', $file);
print '<li><img src="public/slider/'.$file.'" title="'.$name[0].'" /></li>';
}
closedir($handle);
}
?>

关于我如何做到这一点有什么建议吗?

谢谢

最佳答案

试试这个:

<?php

//will hold anything that you want displayed
$content = array(
array('type' => 'yt', 'content' => 'http://www.youtube.com/watch?v=nn2FB1P_Mn8'),
array('type' => 'yt', 'content' => 'http://www.youtube.com/watch?v=8mwKq7_JlS8')
);

if ($handle = opendir('public/slider/'))
{
while (false !== ($file = readdir($handle)))
{
$content[] = array('type' => 'img', 'content' => $file);
}

closedir($handle);
}



//$content = array_slice($content, 2);

foreach ($content as $liItem)
{
switch($liItem['type']){
case 'img':
$name = explode('.', $liItem['content']);
print '<li><img src="public/slider/'.$liItem['content'].'" title="'.$name[0].'" /></li>';
break;
case 'yt':
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $liItem['content'], $match)) {
echo '<li><iframe width="420" height="315" src="http://www.youtube.com/embed/'.$match[1].'" frameborder="0" allowfullscreen></iframe></li>';
}
break;
}


}


?>

关于php - 将 Youtube 链接添加到 php 循环中的 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12179930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com