Validate a Vimeo clip url
Asked Answered
D

4

5

I'd like to validate a field in a form to make sure it contains the proper formatting for a URL linking to a Vimeo video. Below is what I have in Javascript, but I need to convert this over to PHP (not my forte)

Basically, I need to check the field and if it is incorrectly formatted, I need to store an error message as a variable.. if it is correct, I store the variable empty.

// Parse the URL
var PreviewID = jQuery("#customfields-tf-1-tf").val().match(/http:\/\/(www.vimeo|vimeo)\.com(\/|\/clip:)(\d+)(.*?)/);
if ( !PreviewID ) {
    jQuery("#cleaner").html('<div id="vvqvideopreview"><?php echo $this->js_escape( __("Unable to parse preview URL. Please make sure it's the <strong>full</strong> URL and a valid one at that.", 'vipers-video-quicktags') ); ?></div>');
    return;
}

The traditional vimeo url looks like this: http://www.vimeo.com/10793773

Dissuasion answered 18/4, 2010 at 14:13 Comment(0)
F
4
if (0 === preg_match('/^http:\/\/(www\.)?vimeo\.com\/(clip\:)?(\d+).*$/', $value))
{
    $error = 'Unable to parse preview URL';
}

Update, in reply to your comment:

if (0 === preg_match('/^http:\/\/(www\.)?vimeo\.com\/(clip\:)?(\d+).*$/', $value, $match))
{
    $error = 'the error';
}
else
{
    $vimeoID = $match[3];
}
Flacon answered 18/4, 2010 at 14:24 Comment(1)
Thanks for the quick response! Can you tell me how I would strip the user input to set only the vimeo id number as a variable?Dissuasion
M
2

Just parse your $_REQUEST with preg_match like.

$vimeo_array = array();
$vimeo_link = $_REQUEST("form_input_name");
if(preg_match(/http:\/\/(www.vimeo|vimeo)\.com(\/|\/clip:)(\d+)(.*?)/, $vimeo_array, $arr))
{
  $vimeo_code = $vimeo_array[3];
} else {
  $error = "Not a valid link";
}
Markhor answered 18/4, 2010 at 14:34 Comment(2)
Thanks for the quick response! Can you tell me how I would strip the user input to set only the vimeo id number as a variable?Dissuasion
You can use modified regular expresion from salathe's answer ('~^http://(?:www\.)?vimeo\.com/(?:clip:)?(\d+)~'), then the id will be saved in $vimeo_array[1].Markhor
F
2

Try this for https / http url

if (preg_match('/^(http|https):\/\/(www\.)?vimeo\.com\/(clip\:)?(\d+).*$/', $vimeo_url, $vimeo_id)){
    $vimeoid = $vimeo_id[4];
}else{
   // error message... 
}
Falcon answered 27/6, 2012 at 13:43 Comment(0)
C
0

To get at the Vimeo ID number, you could do something like the following:

$link = 'http://vimeo.com/10638288';
if (preg_match('~^http://(?:www\.)?vimeo\.com/(?:clip:)?(\d+)~', $link, $match)) {
    $vimeo_id = $match[1];
} else {
    // Show user an error, perhaps
}

I also slightly altered the regular expression, to save excessive backslash escape characters.

Cycle answered 18/4, 2010 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.