我正在创建一个藤蔓脚本,我正在尝试从我的脚本设置的选定藤蔓视频 url 中收集缩略图.jpg
如下所示,它是在我的og:image:secure_url
上调用的
<meta property="og:image:secure_url" content="{php} echo vine_pic($this->_tpl_vars['p']['youtube_key']);{/php}" />
我需要帮助的内容
设置147
字符的string limit
。因为当拇指从藤蔓视频网址生成时,它们看起来像这样。
https://v.cdn.vine.co/r/thumbs/6A6EB338-0961-4382-9D0D-E58CC705C8D5-2536-00000172EBB64B1B_1f3e673a8d2.1.3.mp4.jpg?versionId=i7r_pcP2P1noapLmoI0QgrtvsD8ii43f
如果og:image:secure_url
包含列出的额外字符,则无法正确读取
?versionId=i7r_pcP2P1noapLmoI0QgrtvsD8ii43f
我的代码将字符串限制放入
function vine_pic( $id )
{
$vine = file_get_contents("http://vine.co/v/{$id}");
preg_match('/property="og:image" content="(.*?)"/', $vine, $matches);
return ($matches[1]) ? $matches[1] : false;
// As you see below, I made an attempt but it doesn't work.
substr(0, 147, $vine, $matches);
}
您的substr()
语法不正确。
它实际上应该是:
substr ($string, $start, $length)
要使用 substr()
,您需要将缩略图 URL 存储在变量中,如下所示:
function vine_pic( $id )
{
$vine = file_get_contents("http://vine.co/v/{$id}");
preg_match('/property="og:image" content="(.*?)"/', $vine, $matches);
$thumb = ($matches[1]) ? $matches[1] : false;
$thumb = substr($thumb, 0, 147);
return $thumb;
}
在尝试使用substr()
之前检查是否设置了$thumb
可能是个好主意:
if ($thumb) {
$thumb = substr($thumb, 0, 147);
return $thumb;
}