Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple PHP Regex question

Tags:

regex

php

vimeo

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

Thanks!

like image 965
Dave Kiss Avatar asked Feb 27 '23 04:02

Dave Kiss


2 Answers

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];
}
like image 163
Amy B Avatar answered Mar 09 '23 00:03

Amy B


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";
}
like image 42
retro Avatar answered Mar 08 '23 23:03

retro