Simple PHP Regex Question

I would like to check the field on the form to make sure it contains the correct formatting for the URL associated with the Vimeo video. The following is what I have in Javascript, but I need to convert this to PHP (not my single)

Basically, I need to check this field, and if it is not formatted correctly, I need to save the error message as a variable. If this is correct, I keep 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 the <strong>full</strong> URL and a valid one at that.", 'vipers-video-quicktags') ); ?></div>'); return; } 

The traditional vimeo URL is as follows: http://www.vimeo.com/10793773

Thanks!

+4
source share
4 answers
 if (0 === preg_match('/^http:\/\/(www\.)?vimeo\.com\/(clip\:)?(\d+).*$/', $value)) { $error = 'Unable to parse preview URL'; } 

Refresh in response to your comment:

 if (0 === preg_match('/^http:\/\/(www\.)?vimeo\.com\/(clip\:)?(\d+).*$/', $value, $match)) { $error = 'the error'; } else { $vimeoID = $match[3]; } 
+4
source

Just parse your $ _REQUEST using 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"; } 
+2
source

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... } 
+2
source

To get the Vimeo ID, you can 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 modified the regex to preserve the extra backslash characters.

0
source

Source: https://habr.com/ru/post/1307271/


All Articles