是PHP还是Joomla!问题(或者我可能遗漏了一些关于输出缓冲的内容)


Is it a PHP or Joomla! issue? (or perhaps I'm missing something about the output buffering)

我正在尝试将VideoJS集成到一个插件中,用于在博客和文章视图/布局中显示视频。首先,我发现插件标签使用正则表达式和视频源,可以是YouTube视频ID或视频文件的路径:

YouTube视频来源:{youtube}y0u7u83v1de0{/youtube}视频文件来源:{video}path/到/video.mp4{/video}

找到这些值不是问题,问题出现在尝试回显视频源时。我使用一个stdClass对象来保存onContentBeforeDisplay函数中的值:

$width = 636; 
$height = 333; 
$youtubeCode = '/{youtube}(.*?){'/youtube}/'; 
$videoCode = '/{video}(.*?){'/video}/'; 
preg_match($youtubeCode, $article->introtext, $match); 
preg_match($videoCode, $article->introtext, $match); 
$video = new stdClass(); 
$video->source = $match[1]; 
$video->width = $width; 
$video->height = $height; 
$layout = JPATH_SITE . DS . 'plugins' . DS . $this->plugin->type . 
DS . $this->plugin->name . DS . 'tmpl' . DS . 'default.php'; 
if ($layout) { 
    ob_start(); 
    require $layout; 
    $contents = ob_get_contents(); 
    ob_end_clean(); 
    $article->introtext = $contents . $article->introtext; 
}

现在,布局文件只输出带有各自的值:

<video id="<?= $video->id ?>" class="video-js vjs-default-skin" controls width="<?= $video->width ?>" height="<?= $video->height ?>" poster="<?= $video->images['preview'] ?>" data-setup="{techOrder: ['youtube', 'html5']}">
    <source src="<?= $video->source ?>" type="<?= $video->format ?>" /> 
</video>

除了$video->source属性外,所有值都显示正确,该属性在启动输出缓冲之前仍然具有正确的值,但似乎启动输出会使该特定值无效。

是什么导致了这种行为?我可能缺少一些关于输出缓冲的内容?

提前感谢!

我不知道VideoJS,也不知道你到底想做什么,但根据我所看到的,将$video->source设置为$match[1],但你首先测试正则表达式来搜索{youtube}标签,然后搜索{video}标签,但你使用相同的$match变量,并且你不测试你(最终)找到的是(youtube视频还是"普通"视频?)。

从我所看到的,你访问布局文件中的$video->格式,但你没有设置它。

也许你应该写一些这样的东西

[...]
if ( preg_match($youtubeCode, $article->introtext, $match) ) {
    // found a {youtube} tag
    $video->format = "youtube";
    $video->source = $match[1];
} else if ( preg_match($videoCode, $article->introtext, $match) ) {
    // found a {video} tag
    $video->format = "video";
    $video->source = $match[1];
}
// Prepend the <video> element if {youtube} or {video} tag found
if ( $layout && !is_null($video->format) && !is_null($video->format) ) { 
    ob_start(); 
    require $layout; 
    $contents = ob_get_contents(); 
    ob_end_clean(); 
    $article->introtext = $contents . $article->introtext; 
}

但请注意,因为它应该只替换找到的第一个标签,如果你有一篇文章有几个{youtube}/{video}标签,它应该不起作用。

希望我已经很好地理解了你要做的事情:p