3

I need to get the vine video id from the url

so the output from link like this

https://vine.co/v/bXidIgMnIPJ

be like this

bXidIgMnIPJ

I tried to use code form other question here for Vimeo (NOT VINE) Get img thumbnails from Vimeo?

This what I tried to use but I did not succeed

$url = 'https://vine.co/v/bXidIgMnIPJ';

preg_replace('~^https://(?:www\.)?vine\.co/(?:clip:)?(\d+)~','$1',$url)
Community
  • 1
  • 1
Jim
  • 1,430
  • 5
  • 18
  • 41

6 Answers6

3

basename maybe?

<?php
$url = 'https://vine.co/v/bXidIgMnIPJ';
var_dump(basename($url));    

http://codepad.org/vZiFP27y

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
  • Excuse me if my question is stupid, Why the output not just the video id? – Jim Jun 24 '13 at 22:58
  • I am just using var_dump to show the result of the basename function. You could just say `$id = basename($url);` and you would have the id. – Jonathan Kuhn Jun 24 '13 at 23:01
  • are you referring to the extra `string(11)...etc` in the output? That is from var_dump. [var_dump](http://www.php.net/var_dump) will dump some information about variable. In this case, the variable was a "string" that was "11" characters in length with the value of "bXidIgMnIPJ". var_dump is very useful way of outputting data about a variable. If the string was empty, echo or print would just output nothing. var_dump actually would say `string(0) ""` which tells me more. var_dump also works with all variable types (boolean, object, array...etc). – Jonathan Kuhn Jun 24 '13 at 23:05
  • Thank You, I wish you all the best :) – Jim Jun 24 '13 at 23:09
1

Assuming it will always be in that format, you can just split the url by the / delimiter. Regex is not needed for a simple url such as this.

$id = end(explode('/', $url));
Austin Brunkhorst
  • 20,704
  • 6
  • 47
  • 61
1

Referring to as the question is asked here is a solution for preg_replace:

$s = 'https://vine.co/v/bXidIgMnIPJ';
$new_s = preg_replace('/^.*\//','',$s);
echo $new_s;
// => bXidIgMnIPJ

or if you need to validate that an input string is indeed a link to vine.co :

$new_s = preg_replace('/^(https?:\/\/)?(www\.)?vine\.co.*\//','',$s);

I don't know if that /v/ part is always present or is it always v... if it is then it may also be added to regex for stricter validation:

$new_s = preg_replace('/^(https?:\/\/)?(www\.)?vine\.co\/v\//','',$s);
trushkevich
  • 2,657
  • 1
  • 28
  • 37
1

Here's what I am using:

function getVineId($url) {
  preg_match("#(?<=vine.co/v/)[0-9A-Za-z]+#", $url, $matches);
  if (isset($matches[0])) {
    return $matches[0];
  }
  return false;
}

I used a look-behind to ensure "vine.co/v/" always precedes the ID, while ignoring if the url is HTTP or HTTPS (or if it lacks a protocol altogether). It assumes the ID is alphanumeric, of any length. It will ignore any characters or parameters after the id (like Google campaign tracking parameters, etc).

I used the "#" delimiter so I wouldn't have to escape the forward slashes (/), for a cleaner look.

thaddeusmt
  • 15,410
  • 9
  • 67
  • 67
0

explode the string with '/' and the last string is what you are looking for :) Code:

$vars = explode("/",$url);
echo $vars[count($vars)-1];
Fallen
  • 4,435
  • 2
  • 26
  • 46
0
$url = 'https://vine.co/v/b2PFre2auF5';
$regex = '/^http(?:s?):\/\/(?:www\.)?vine\.co\/v\/([a-zA-Z0-9]{1,13})$/';
preg_match($regex,$url,$m);
print_r($m);

1. b2PFre2auF5
chris mccoy
  • 327
  • 1
  • 4
  • 11