Nelson
Nelson

Reputation: 38

Setting String Character Limit PHP

I am creating a vine script, and I am trying to collect the thumbnail .jpg from a selected vine video url set by my script as seen below is how it's called on my og:image:secure_url

<meta property="og:image:secure_url" content="{php} echo vine_pic($this->_tpl_vars['p']['youtube_key']);{/php}" />

What I need help with

Setting a string limit of 147 Characters. Because when thumbs are generated from the vine video url they appear like this..

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 will not read right if it contains the extra characters as listed

?versionId=i7r_pcP2P1noapLmoI0QgrtvsD8ii43f

My Code to Put the string limit in

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);

}

Upvotes: 0

Views: 3648

Answers (1)

Amal
Amal

Reputation: 76646

Your substr() syntax is incorrect.

It should actually be:

substr ($string, $start, $length)

To use substr(), you'll need to store the thumbnail URL in a variable, like so:

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;
}

It might be a good idea to check if $thumb is set before trying to use substr():

if ($thumb) {
    $thumb = substr($thumb, 0, 147);
    return $thumb;
}

Upvotes: 4

Related Questions